-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstable sort.cpp
More file actions
68 lines (59 loc) · 1.12 KB
/
stable sort.cpp
File metadata and controls
68 lines (59 loc) · 1.12 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
#include<iostream>
#include<limits.h>
using namespace std;
void insert_sort(int* arr,int n){ //Both sorting algo. are Stable sort
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++)
if(arr[i]>=arr[j])
continue;
else{
int temp=arr[i];
int front=j;
int rear=i;
while(front!=rear){
arr[rear]=arr[rear-1];
rear--;
}
arr[j]=temp;
}
}
}
void bubble_sort(int* arr,int n){
int i=n-1;
while(i!=0){
int temp;
int max=INT_MIN;
for(int j=0;j<=i;j++){
if(arr[j]>max){
max=arr[j];
temp=j;
}
}
int rear=temp;
int front=i;
while(front!=rear){
arr[rear]=arr[rear+1];
rear++;
}
arr[i]=max;
i--;
}
}
int main(){
int arr[]={3,5,4,7,2,9,11,1,2,6,3};
int x=sizeof(arr)/sizeof(arr[0]);
cout<<"Array elements: ";
for(int i=0;i<x;i++)
cout<<arr[i]<<" ";
insert_sort(arr,x);
cout<<"\n\n";
cout<<"Insertion_sort: ";
for(int i=0;i<x;i++)
cout<<arr[i]<<" ";
bubble_sort(arr,x);
cout<<"\n\n";
cout<<"Bubble_sort: ";
for(int i=0;i<x;i++)
cout<<arr[i]<<" ";
cout<<"\n";
}