Skip to content

Commit f2d94c9

Browse files
committed
Added Sorting algorithms in C++
1 parent 9f9ce3f commit f2d94c9

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

data_structures/C++/QUICKSOR.CPP

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//quick sort program time complexity in average case O(nlogn)
2+
3+
#include<iostream>
4+
using namespace std;
5+
void quicksort (int[], int, int);
6+
int main ()
7+
{
8+
int a[50], n, i;
9+
cout<<"enter number of elemnts that u want to store:\n";
10+
cin>>n;
11+
for (i = 0; i < n; i++)
12+
{
13+
cin>>a[i];
14+
}
15+
quicksort (a, 0, n - 1);
16+
for (i = 0; i < n; i++)
17+
{
18+
cout<<"\t"<<a[i];
19+
}
20+
return 0;
21+
}
22+
23+
void
24+
quicksort (int a[], int low, int high)
25+
{
26+
int pivot, i, j, temp;
27+
if (low < high)
28+
{
29+
pivot = low;
30+
i = low;
31+
j = high;
32+
while (i < j)
33+
{
34+
while (a[i] <= a[pivot] && i <= high)
35+
{
36+
i++;
37+
}
38+
while (a[j] > a[pivot] && j >= low)
39+
{
40+
j--;
41+
}
42+
if (i < j)
43+
{
44+
temp = a[i];
45+
a[i] = a[j];
46+
a[j] = temp;
47+
}
48+
}
49+
temp = a[j];
50+
a[j] = a[pivot];
51+
a[pivot] = temp;
52+
quicksort (a, low, j - 1);
53+
quicksort (a, j + 1, high);
54+
}
55+
}

data_structures/C++/SELECTIO.CPP

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Selection sort algorithm works with an time complexity of O(n^2)
2+
#include<iostream>
3+
using namespace std;
4+
int main ()
5+
{
6+
int a[50], n, i, j, min, temp;
7+
cout<<"enter number of elements that u want to store:\n";
8+
cin>>n;
9+
for (i = 0; i < n; i++)
10+
{
11+
cin>>a[i];
12+
}
13+
for (i = 0; i < n - 1; i++)
14+
{
15+
min = i;
16+
for (j = i + 1; j < n; j++)
17+
{
18+
if (a[min] > a[j])
19+
{
20+
min = j;
21+
}
22+
}
23+
if (min != i)
24+
{
25+
temp = a[i];
26+
a[i] = a[min];
27+
a[min] = temp;
28+
}
29+
}
30+
for (i = 0; i < n; i++)
31+
{
32+
cout<<"\t"<<a[i];
33+
}
34+
return 0;
35+
}

data_structures/C++/SHELLSOR.CPP

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
// Shell sort algorithm time complexity is best case O(nlogn) and worst case O(n^2)
3+
4+
#include<iostream>
5+
using namespace std;
6+
class shellsort
7+
{
8+
private:
9+
int a[20], n, i, j, gap, temp;
10+
public:
11+
void getdata ()
12+
{
13+
cout << "enter number of elemnts:\n";
14+
cin >> n;
15+
cout << "enter the elements:\n";
16+
for (i = 0; i < n; i++)
17+
{
18+
cin >> a[i];
19+
}
20+
}
21+
void shellsorting ()
22+
{
23+
24+
for (gap = n / 2; gap > 0; gap = gap / 2)
25+
{
26+
for (j = gap; j < n; j++)
27+
{
28+
for (i = j - gap; i >= 0; i -= gap)
29+
{
30+
if (a[i + gap] > a[i])
31+
{
32+
break;
33+
}
34+
else
35+
{
36+
swap ();
37+
}
38+
}
39+
}
40+
}
41+
}
42+
void swap ()
43+
{
44+
temp = a[i + gap];
45+
a[i + gap] = a[i];
46+
a[i] = temp;
47+
48+
}
49+
void display ()
50+
{
51+
for (i = 0; i < n; i++)
52+
{
53+
cout << a[i] << "\t";
54+
}
55+
}
56+
};
57+
58+
int main ()
59+
{
60+
61+
shellsort obj;
62+
obj.getdata ();
63+
cout << "before sorting:";
64+
obj.display ();
65+
obj.shellsorting ();
66+
cout << endl << "after sorting:";
67+
obj.display ();
68+
return 0;
69+
}

0 commit comments

Comments
 (0)