-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSORT.cpp
More file actions
61 lines (56 loc) · 969 Bytes
/
TSORT.cpp
File metadata and controls
61 lines (56 loc) · 969 Bytes
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
#include<iostream>
#include<stdlib.h>
#include<cstdio>
#define MAX 2147483
using namespace std;
int arr[MAX];//={10,9,8,7,6,5,4,3,2,1};
void swap_elements(int *s,int *t)
{
int u=*s;
*s=*t;
*t=u;
}
int partition_array(int beg,int last)
{
int pivot=arr[last];
int i=beg-1;
for(int j=beg;j<last;j++)
{
if(arr[j]<=pivot)
{
i++;
swap_elements(&arr[i],&arr[j]);
}
}
swap_elements(&arr[i+1],&arr[last]);
return (i+1);
}
void quick_sort(int beg,int last)
{
if(beg<last)
{
int pi=partition_array(beg,last);
quick_sort(beg,pi-1);
quick_sort(pi+1,last);
}
}
void display(int number)
{
//cout<<"Elements after sorting"<<endl;
for(int i=0;i<number;i++)
{
cout<<arr[i]<<endl;\
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quick_sort(0,n-1);
display(n);
return 0;
}