We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Quicksort 2 - Sorting
- Discussions
Quicksort 2 - Sorting
Quicksort 2 - Sorting
Sort by
recency
|
203 Discussions
|
Please Login in order to post a comment
/* code in c */
include
include
include
include
include
void quickSort(int *ar, int size)
{
if(size > 1){
int temp, pivot = ar[0], ub = size-1;
for(int i = size-1; i >= 0; --i){
if(ar[i] >= pivot){
temp = ar[i];
int j = i;
while(j < ub){//Variation of Insertion Sorting
ar[j] = ar[j+1];
++j;
}
ar[ub--] = temp;
}
}
++ub;
quickSort(ar, ub);
quickSort(ar+ub+1, size-ub-1);
//Output
for(int i = 0; i < size; ++i){
printf("%d ", ar[i]);
}
printf("\n");
}
}
int main(void)
{
int N;
scanf("%d", &N);
int ar[N];
for(int i = 0; i < N; i++){
scanf("%d", &ar[i]);
}
quickSort(ar, N);
return 0;
}
In the previous challenge, you wrote a partition method to split an array into two sub-arrays, one containing smaller elements and one containing larger elements than a given number. This means you 'sorted' half the array with respect to the other half. Can you repeatedly use partition to sort an entire array?
Guideline In Insertion Sort, you simply went through each element in order and inserted it into a sorted sub-array. In this challenge, you cannot focus on one element at a time, but instead must deal with whole sub-arrays, with a strategy known as "divide and conquer".
When partition is called on an array, two parts of the array get 'sorted' with respect to each other. If partition is then called on each sub-array, the array will now be split into four parts. This process can be repeated until the sub-arrays are small. Notice that when partition is called on just one of the numbers, they end up being sorted.
Can you repeatedly call partition so that the entire array ends up sorted?
Print Sub-Arrays In this challenge, print your array every time your partitioning method finishes, i.e. whenever two subarrays, along with the pivot, are merged together.
The first element in a sub-array should be used as a pivot. Partition the left side before partitioning the right side. The pivot should be placed between sub-arrays while merging them. Array of length or less will be considered sorted, and there is no need to sort or to print them. Note Please maintain the original order of the elements in the left and right partitions while partitioning around a pivot element.
For example: Partition about the first element for the array A[]={5, 8, 1, 3, 7, 9, 2} will be {1, 3, 2, 5, 8, 7, 9}
Input Format There will be two lines of input:
Constraints
Each number is unique.
Sample Input
7 5 8 1 3 7 9 2 Sample Output
2 3 1 2 3 7 8 9 1 2 3 5 7 8 9 Explanation This is a diagram of Quicksort operating on the sample array:
quick-sort diagram
python3
This solution is going crazy for me: I have tried like this
I have this code:
And output is like this:
if anybody knows please suggests me!!
`
Python