PROGRAM FOR SORTING ARRAY USING QUICK SORT.
#include <iostream>
#include <conio.h>
using namespace std;
class Array
{
private:
int *Arr, Size, Right;
public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
Arr[I] = 0;
Right = 0;
Size = Siz;
}
void Input()
{
int No;
cout << "Enter Size Of Array: ";
cin >> No;
Right = No;
cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I <Right; I++)
cin >> Arr[I];
}
void Quick_Sort()
{
Sort(Arr, 0, Right -1);
}
void Sort(int Temp[], int Beg, int End)
{
if(Beg >= End)
return;
int Mid = Partition(Temp, Beg, End);
Sort(Temp, Beg, Mid -1);
Sort(Temp, Mid +1, End);
}
int Partition (int Temp[], int Beg, int End)
{
int Pivot = Temp[Beg];
int Index = Beg ;
for (int J = Beg +1; J <= End; J++)
{
if (Temp[J] < Pivot)
{
Index++;
Swap(&Temp[Index], &Temp[J]);
}
}
Swap(&Temp[Index], &Temp[Beg]);
return Index;
}
void Swap(int *Value1, int *Value2)
{
int Temp = *Value1;
*Value1 = *Value2;
*Value2 = Temp;
}
void Print()
{
cout << "\nAfter Sorting Elements Of Array is: " << endl;
for (int I = 0; I <Right; I++)
cout << Arr[I] << endl;
}
~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};
int main()
{
Array Obj(20);
Obj.Input();
Obj.Quick_Sort();
Obj.Print();
getch();
return 0;
}
0 comments:
Post a Comment