forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpancakeSort.cpp
More file actions
80 lines (53 loc) · 1.25 KB
/
pancakeSort.cpp
File metadata and controls
80 lines (53 loc) · 1.25 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
int maxArr(int arr[], int last) {
int max = 0;
for(int i = 1; i < last; ++i) {
if(arr[max] < arr[i]) {
max = i;
}
}
return max;
}
void swapArr(int arr[], int index) {
int index1 = 0;
while(index1 < index) {
int temp = arr[index1];
arr[index1] = arr[index];
arr[index] = temp;
index1++;
index--;
}
}
void showArr(int arr[], int size) {
for(int i = 0; i < size; ++i) {
cout<<arr[i]<<" ";
}
cout<<"\n";
}
void pancakeSort(int arr[], int size) {
for(int i = size-1; i > 0 ; --i) {
int index = maxArr(arr, i);
if(i != index) {
swapArr(arr, index);
swapArr(arr, i);
}
}
}
int main() {
int n;
cin>>n;
int arr[n];
for(int i = 0; i < n; ++i) {
cin>>arr[i];
}
cout<<"Unsorted array : ";
showArr(arr, n);
pancakeSort(arr, n);
cout<<"Sorted Array : ";
showArr(arr, n);
}
/*
Unsorted array : 54 85 52 25 98 75 25 11 68
Sorted Array : 11 25 25 52 54 68 75 85 98
*/