الجمعة، 16 مايو 2014
3:20 ص

quick_sort

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class quicksort
{
public:
int no_of_elements;
int elements[10];
public:
void getarray();
void sortit(int[], int, int);
void partition(int[], int, int,int &);
void display();
};
void quicksort::getarray(){
cout << "how many elements ?: ";
cin >> no_of_elements;
cout << "insert array of element to sort : ";
for (int i = 0; i < no_of_elements; i++)
cin >> elements[i];
}
void quicksort::sortit(int x[], int lb, int ub)
{
int j;
if (lb >= ub)
return;
display();
partition(x, lb, ub, j);
sortit(x, lb, j - 1);
sortit(x, j + 1, ub);
}
void quicksort::partition(int x[], int lb, int ub, int &pj)
{
int a, down, temp, up;
a = x[lb];
up = ub;
down = lb;
while (down < up)
{
while (x[down] <= a)
down++;
while (x[up]>a)
up--;
if (down < up)
{
temp = x[down];
x[down] = x[up];
x[up] = temp;
}
}
x[lb] = x[up];
x[up] = a;
pj = up;
}
void quicksort::display()
{
for (int i = 0; i < no_of_elements; i++)
cout << elements[i] << " ";
cout << endl;
}

void main()
{
quicksort qs;
qs.getarray();
cout << "sort is given in step by step showing each iteration" << endl;
qs.sortit(qs.elements, 0, qs.no_of_elements - 1);
qs.display();
_getch();
}

0 التعليقات:

إرسال تعليق