Quicksort
Time limit: 5000ms
Memory limit: 256mb
Description:
Please complete the missing code of the quicksort.
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int RANDOM(int left, int right);
void swap(int arr[], int posA, int posB);
void quicksort(int arr[], int left, int right);
int partition(int arr[], int left, int right, int pivot);
int RANDOM(int left, int right){
if (left >= right){
return left;
}
return (rand()%(right - left) + left);
}
void swap(int arr[], int posA, int posB){
int tmp = arr[posA];
arr[posA] = arr[posB];
arr[posB] = tmp;
}
void quicksort(int arr[], int left, int right){
if (left >= right){
return ;
}
int pivot = RANDOM(left, right);
int newPos= partition(arr, left, right, pivot);
quicksort(arr, left, newPos - 1);
quicksort(arr, newPos + 1, right);
}
int partition(int arr[], int left, int right, int pivot){
int pVal = arr[pivot];
swap(arr, pivot, right);
//write down your code here
}
int main(void) {
srand(time(NULL));
int N = 0;
scanf("%d", &N);
int *array = (int *) malloc(N * sizeof(int));
for(int i = 0; i < N; i++) {
scanf("%d", &array[i]);
}
quicksort(array, 0, N - 1);
for (int i = 0; i < N; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
-------------------------------------------End of Code-------------------------------------------
Input:
The first line contains an integer N > 0;
The second line is an unsorted array of N unique integers.
Output:
Please output the sorted version of the array in ascending order.
Sample Input 1:
6
1 3 2 5 4 6
Sample Output 1:
1 2 3 4 5 6
Sample Input 2:
8
11 13 8 7 6 5 22 18
Sample Output 2:
5 6 7 8 11 13 18 22
Submit