Looking for help on my Python programming assignment, any help would be great! P
ID: 3709522 • Letter: L
Question
Looking for help on my Python programming assignment, any help would be great!
Problem Description: Traditional recursive implementation of QuickSort uses two functions. Your task is to implement recursive QuickSort using a single function. Implementation Details: You will write two functions: ‘Quicksort' and 'main. The latter will be your driver function to test the QuickSort function. Inside the main function, randomly generate ten different lists of integers between -20 and +20. The lengths of these lists should be randomly selected to be between 10 and 15. Then run your implementation of QuickSort on each list, and numerically verify that sorting was correct (hint: use the numpy.any and numpy.ediff methods from the numpy module for verifying that sorting has worked). You should then display a message indicating whether sorting was successful for each of these lists.Explanation / Answer
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
for(int i=1;i<=10;i++)
{
int size=rand()%6 + 10;
int arr[size+5];
for(int j=0;j<size;j++)
arr[j]=rand()%41 - 20;
int flag=quicksort(arr,0,size-1);
if (flag==1)
printf("Sorting sucessful");
}
}
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
for(int i=1;i<=10;i++)
{
int size=rand()%6 + 10;
int arr[size+5];
for(int j=0;j<size;j++)
arr[j]=rand()%41 - 20;
int flag=quicksort(arr,0,size-1);
if (flag==1)
printf("Sorting sucessful");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.