Skip to content

Instantly share code, notes, and snippets.

@cozyplanes
Last active August 19, 2017 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozyplanes/8361f75f1030a2dde01f1e775ec359b0 to your computer and use it in GitHub Desktop.
Save cozyplanes/8361f75f1030a2dde01f1e775ec359b0 to your computer and use it in GitHub Desktop.
Quick Sort 알고리즘 입니다.
#include <stdio.h>
#include <stdbool.h>
#define MAX 9
int intArray[MAX] = {15,25,45,65,22,33,9,7,55};
void printline(int count) {
int i;
for(i = 0;i <count-1;i++) {
printf("=");
}
printf("=\n");
}
void display() {
int i;
printf("[");
// navigate through all items
for(i = 0;i<MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}
void swap(int num1, int num2) {
int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int pivot) {
int leftPointer = left -1;
int rightPointer = right;
while(true) {
while(intArray[++leftPointer] < pivot) {
//do nothing
}
while(rightPointer > 0 && intArray[--rightPointer] > pivot) {
//do nothing
}
if(leftPointer >= rightPointer) {
break;
} else {
printf(" item swapped :%d,%d\n", intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
}
}
printf(" pivot swapped :%d,%d\n", intArray[leftPointer],intArray[right]);
swap(leftPointer,right);
printf("Updated Array: ");
display();
return leftPointer;
}
void quickSort(int left, int right) {
if(right-left <= 0) {
return;
} else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}
main() {
printf("Input Array: ");
display();
printline(50);
quickSort(0,MAX-1);
printf("Output Array: ");
display();
printline(50);
}
@cozyplanes
Copy link
Author

cozyplanes commented Aug 19, 2017

Input Array: [15 25 45 65 22 33 9 7 55 ]
==================================================
 item swapped :65,7
 pivot swapped :65,55
Updated Array: [15 25 45 7 22 33 9 55 65 ]
 item swapped :15,7
 pivot swapped :25,9
Updated Array: [7 9 45 15 22 33 25 55 65 ]
 item swapped :45,22
 pivot swapped :45,25
Updated Array: [7 9 22 15 25 33 45 55 65 ]
 pivot swapped :22,15
Updated Array: [7 9 15 22 25 33 45 55 65 ]
 pivot swapped :45,45
Updated Array: [7 9 15 22 25 33 45 55 65 ]
Output Array: [7 9 15 22 25 33 45 55 65 ]
==================================================

Process returned 0 (0x0)   execution time : 0.043 s
Press any key to continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment