-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSorting.cpp
More file actions
160 lines (149 loc) · 2.69 KB
/
Sorting.cpp
File metadata and controls
160 lines (149 loc) · 2.69 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include<iostream>
using namespace std;
void swap(int &a , int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int bubbleSort(int a[] ,int n)
{
int temp,countBS=0;
for(int i=0;i<n-(n/2)+1;i++)
{
countBS++;
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after pass: "<<i+1<<"\t";
for(int k=0;k<n;k++)
{
cout<<a[k]<<"\t";
}
cout<<endl;
}
cout<<"Number of Swaps: "<<countBS<<endl;
cout<<"Sorted Array:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
return countBS;
}
int selectionSort(int a[],int n)
{
int countSS=0;
for(int i=0;i<n-1;i++)
{ countSS++;
int min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
swap(a[i],a[min]);
cout<<"Array after pass: "<<i+1<<"\t";
for(int k=0;k<n;k++)
{
cout<<a[k]<<"\t";
}
cout<<endl;
}
cout<<"Number of Swaps: "<<countSS<<endl;
cout<<"Sorted Array:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
return countSS;
}
int insertionSort(int a[],int n)
{
int j,item,countIS=0;
for(int i=1;i<n;i++)
{
countIS++;
item=a[i];
j=i-1;
while(j>=0 && a[j]>item)
{
a[j+1]=a[j];
j--;
}
a[j+1]=item;
cout<<"Array after pass: "<<i<<"\t";
for(int k=0;k<n;k++)
{
cout<<a[k]<<"\t";
}
cout<<endl;
}
cout<<"";
cout<<"Number of Swaps: "<<countIS<<endl;
cout<<"Sorted Array:"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
return countIS;
}
int main()
{
int size,n,bs,is,ss;
cout<<"Enter the Size of the Array: ";
cin>>size;
cout<<"Enter the Number of elements in the Array: ";
cin>>n;
int arr[size],arr1[size],arr2[size];
//Inputing data in Array
cout<<"Enter the Elements in the Array: "<<endl;
for(int i=0;i<n;i++)
{
cout<<"Enter the Value of a["<<i<<"]: ";
cin>>arr[i];
}
//Original Array
cout<<"original Array: "<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
}
for(int i=0;i<n;i++)
{
arr1[i]=arr[i];
arr2[i]=arr[i];
}
cout<<endl;
cout<<endl<<"Bubble Sort: "<<endl;
bs=bubbleSort(arr,n);
cout<<endl<<"Insertion Sort: "<<endl;
is=insertionSort(arr1,n);
cout<<endl<<"Selection Sort: "<<endl;
ss=selectionSort(arr2,n);
if(bs<is and bs<ss)
{
cout<<endl<<"Bubble Sort requires less number of passes"<<endl;
}
else if(is<bs and is<ss)
{
cout<<endl<<"Insertion Sort requires less number of passes"<<endl;
}
else if(ss<bs and ss<is)
{
cout<<endl<<"Selection Sort Requires less number of passes"<<endl;
}
else
{
cout<<endl<<"Number of passes are equal in every sort"<<endl;
}
return 0;
}