You can find quality tech news,tricks,study materials etc contact me:facebook.com/ashwin.sunilkumar
Support Me...........
Please support me by giving donation
contact: aswinavofficial@gmail.com
Total Pageviews
Monday, September 1, 2014
C++ program to perform QuickSort
// QuickSort
#include
#include
using namespace std;
void print(int a[]) {
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl << endl;
}
int partition(int a[], int p, int r) {
int x = a[r];
int j = p - 1;
for (int i = p; i < r; i++) {
if (x <= a[i]) {
j = j + 1;
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
a[r] = a[j + 1];
a[j + 1] = x;
return (j + 1);
}
void quickSort(int a[], int p, int r) {
if (p < r) {
int q = partition(a, p, r);
quickSort(a, p, q - 1);
quickSort(a, q + 1, r);
}
}
int main() {
int a[] = {
1, 9, 0, 5, 6, 7, 8, 2, 4, 3};
cout << "Original array: " << endl;
print(a);
quickSort(a, 0, 9);
cout << "Sorted array: " << endl;
print(a);
return 0;
}
// aiuto
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment