Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support user input size,elements #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 28 additions & 27 deletions sorting/quickSort.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@
*/

#include <stdio.h>
#define SIZE 6 //array size


void partitionArray(int *a, int beg, int end, int *pivotLoc);
void quickSort(int *a, int beg, int end);
void partitionArray(int *j, int beg, int end, int *pivotLoc);
void quickSort(int *j, int beg, int end);

int main(){
int a[SIZE] = {5,2,6,1,3,4}; //unsorted array
int i;

quickSort(a, 0, SIZE-1); //beg = 0 start index of array end = 5 last index of array

int k;
printf("How many elements?\t");
scanf("%d",&k);
int i,j[k];
for(i=0;i<k;i++){
scanf("%d",&j[i]);
}
quickSort(j, 0, k-1); //beg = 0 start index of array end = 5 last index of array
//printing sorted element of array
for(i = 0; i < SIZE; i++){
printf("%d\t", a[i]);
for(i = 0; i < k; i++){
printf("%d\t", j[i]);
}
return 0;
}//main() ends here

void quickSort(int *a, int beg, int end){
void quickSort(int *j, int beg, int end){
int pivotLoc;
if(beg < end){
partitionArray(a, beg, end, &pivotLoc); //this will find the pivot location and partition the array
quickSort(a, beg, pivotLoc - 1); //quick sort the left sub array
quickSort(a, pivotLoc + 1, end); //quick sort the right sub array
partitionArray(j, beg, end, &pivotLoc); //this will find the pivot location and partition the array
quickSort(j, beg, pivotLoc - 1); //quick sort the left sub array
quickSort(j, pivotLoc + 1, end); //quick sort the right sub array
}
}//quickSort() ends here

void partitionArray(int *a, int beg, int end, int *pivotLoc){
void partitionArray(int *j, int beg, int end, int *pivotLoc){
int left = beg; //initially left point to the first element of the array
int right = end; //initially right point to the last element of the array
*pivotLoc = left; //initially pivot point to first element of the array
Expand All @@ -41,33 +42,33 @@ void partitionArray(int *a, int beg, int end, int *pivotLoc){
while(1){

//pivot pointing at left
while(a[*pivotLoc] <= a[right] && *pivotLoc != right){ //pivot element <= right element
while(j[*pivotLoc] <= j[right] && *pivotLoc != right){ //pivot element <= right element
right--; //move right one position towards left
}

if(*pivotLoc == right){ //both left and right pointing at same element of the array
break;
}else if(a[*pivotLoc] > a[right]){
}else if(j[*pivotLoc] > j[right]){
//pivot element greater than right element. swap pivot and right element.
tmp = a[right];
a[right] = a[*pivotLoc];
a[*pivotLoc] = tmp;
tmp = j[right];
j[right] = j[*pivotLoc];
j[*pivotLoc] = tmp;
*pivotLoc = right; //pivot is now pointing to right
}

//pivot pointing to right
while(a[*pivotLoc] >= a[left] && *pivotLoc != left){ //pivot element >= left element
while(j[*pivotLoc] >= j[left] && *pivotLoc != left){ //pivot element >= left element
left++; //move left one position towards right
}

if(*pivotLoc == left){ //both left and right pointing at the same element of the array
break;
}else if(a[*pivotLoc] < a[left]){
}else if(j[*pivotLoc] < j[left]){
//pivot element smaller than left element. swap pivot and left element.
tmp = a[left];
a[left] = a[*pivotLoc];
a[*pivotLoc] = tmp;
tmp = j[left];
j[left] = j[*pivotLoc];
j[*pivotLoc] = tmp;
*pivotLoc = left; //pivot is now pointing to left
}
}
}//partitionArray() ends here
}//partitionArray() ends here