-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.cpp
141 lines (126 loc) · 2.55 KB
/
quick_sort.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//quicksort using stacks and queue..........
#include<iostream>
using namespace std;
//sorting algorithm used>>>..........
int quicksort(int *arr,int beg,int end,int loc){
while(beg<=end){
if(loc==end){
if(arr[loc]<arr[beg]){
int temp = arr[beg];
arr[beg]=arr[loc];
arr[loc]=temp;
loc=beg;
end-=1;
//goto label2;
}
else
beg+=1;
//goto label1;
}
//label2:
if(loc==beg){
if(arr[loc]>arr[end]){
int temp = arr[end];
arr[end]=arr[loc];
arr[loc]=temp;
loc=end;
beg+=1;
//goto label1;
}
else
end-=1;
//goto label2;
}
}
return loc;
}
//using stacks...........................
void partition(int arr[],int size){
int stack1[20],stack2[20],top=-1,beg,end,n;
if(size>1){
top+=1;
stack1[top]=0;
stack2[top]=size-1;
while(top!=-1){
beg=stack1[top];
end=stack2[top];
top-=1;
n=quicksort(arr,beg,end,end);
if(beg<n-1){
top+=1;
stack1[top]=beg;
stack2[top]=n-1;
}
if(n<end-1){
top++;
stack1[top]=n+1;
stack2[top]=end;
}
}
}
else
return;
}
//using queue partitioning ............
int insert(int queue[],int size,int &front,int &rear,int item){
if((rear==size-1 && front==0)|| front==rear+1){
cout<<"\noverflow..";
return 0;
}
else if(front==-1){
front++;
rear=front;
}
else if(rear==size-1)
rear=0;
else
rear++;
queue[rear]=item;
return 1;
}
int del(int queue[],int size,int &front,int &rear){
int item;
if(front==-1){
cout<<"\nunderflow.....";
return -1;
}
else{
item=queue[front];
if(front==rear){
front=-1;
rear=-1;
}
else if(front==size-1)
front=0;
else
front++;
}
return item;
}
void partition1(int arr[],int size){
int queue[6],front=-1,rear=-1,k=1,n,beg,end,flag=0;
if(size>1){
k=insert(queue,6,front,rear,0);
k=insert(queue,6,front,rear,size-1);
while(front!=rear){
beg=del(queue,6,front,rear);
end=del(queue,6,front,rear);
n=quicksort(arr,beg,end,end);
if(beg<n-1){
k=insert(queue,6,front,rear,beg);
k=insert(queue,6,front,rear,n-1);
}
if(n<end-1){
k=insert(queue,6,front,rear,n+1);
k=insert(queue,6,front,rear,end);
}
}
}
}
int main(){
int arr[]={9,7,5,11,12,2,14,3,10,6};
partition1(arr,10);
for(int i=0;i<10;i++)
cout<<arr[i]<<"\t";
return 0;
}