-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
64 lines (60 loc) · 1.27 KB
/
quickSort.cpp
File metadata and controls
64 lines (60 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
void quickSort(int*&, int, int);
void sort(int*&, int, int, int&);
void showVec(int[], int);
int main()
{
int* vec;
int size = -1;
do
{
cout << "Enter the size of your vector: ";
cin >> size;
} while (size < 0);
vec = new int[size];
cout << "Please insert value into your vector!" << endl;
for(int i = 0;i<size;i++)
{
cout << "Position " << i+1 << ": ";
cin >> vec[i];
cin.ignore();
}
cout << "Your vector is : " << endl;
showVec(vec, size);
quickSort(vec, 0, size);
cout << "\n ++++++ Here is your sorted vector with quick sort ++++++" << endl;
showVec(vec, size);
delete[] vec;
}
void sort(int*& t, int low, int high, int& place)
{
int i,j,pivot;
pivot = t[low];
i=low+1; j= high;
while(i<=j)
{
if(t[i]<=pivot) i++;
else
{
swap(t[i],t[j]);
j--;
}
}
swap(t[low], t[j]);
place = j;
}
void quickSort(int*& t, int low, int high)
{
int place;
if(low < high)
{
sort(t, low, high, place);
quickSort(t, low, place);
quickSort(t, place+1, high);
}
}
void showVec(int t[], int n)
{
for(int i=0;i<n;i++) cout << t[i] << " ";
}