forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quick_sort.cpp Adds QuickSort Algorithm
Corrected erros from 1st commit. Closes NITSkmOS#250
- Loading branch information
Showing
3 changed files
with
16 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,39 @@ | ||
|
||
#include <iostream> | ||
|
||
using std::cout; | ||
|
||
void quick_sort(int a[], int left, int right) { | ||
int i = left, j = right; | ||
int pivot = a[right]; | ||
|
||
int i = left, j = right; | ||
int pivot = a[right]; | ||
|
||
while (i <= j) { | ||
while (i <= j) { | ||
while (a[i] < pivot) | ||
i++; //carry on, this part already sorted | ||
while (a[j] > pivot) | ||
j--; //carry on, this part already sorted | ||
if (i <= j) { // a[i]>=pivot and a[j]<=pivot | ||
//swap a[i] with a[j] | ||
//swap a[i] with a[j] | ||
int tmp = a[i]; | ||
a[i] = a[j]; | ||
a[j] = tmp; | ||
i++; | ||
j--; | ||
} | ||
}; | ||
} | ||
|
||
if (left < j) | ||
if (left < j) | ||
quick_sort(a, left, j); | ||
if (i < right) | ||
if (i < right) | ||
quick_sort(a, i, right); | ||
|
||
} | ||
|
||
int main(void){ | ||
int main(void) { | ||
int a[10] = { 10, 7, 9, 4, 6, 2, 5, 6, 84, 12}; | ||
int a_size = sizeof(a)/sizeof(a[0]); | ||
|
||
quick_sort(a, 0, a_size-1); | ||
|
||
for(int i=0; i<a_size; i++){ | ||
for (int i = 0; i < a_size; i++) { | ||
cout << a[i] << " "; | ||
} | ||
} | ||
} |