-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.sorting algorithms.cpp
101 lines (86 loc) · 1.91 KB
/
17.sorting algorithms.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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// print array function
void p(int arr[], int n)
{
cout << "printing the Array " << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// selection sort algorithm
// its work on the select the minimum from the array
// swap with first element and after swap with i+1
void selection_sort(int arr[], int n)
{
// for each i=1 j runs n times
for (int i = 0; i < n; i++)
{
// assume the minimum value is at i
int min_index = i;
for (int j = i; j < n; j++)
{
if (arr[j] < arr[min_index])
{
min_index = j;
}
}
// swap function
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
// we can use in built function as well works same
// swap(arr[i], arr[min_index]);
}
}
void bubble_Sort(int arr[], int n)
{
//write your code here
for (int i = 0; i < n; i++)
{
int didswap = 0;
for (int j = 0; j < n-1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
didswap = 1;
}
}
// if the array is sorted its break it
if(didswap == 0) {
break;
}
}
}
void insertion_sort(int arr[], int n)
{
for (int i = 0; i <=n-1; i++)
{ int j = i;
while (j > 0 && arr[j-1] > arr[j])
{
// swap function
int temp;
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
j--;
}
}
}
int main()
{
int arr[] = {90, 5, 12, 34, 56, 7, 9};
int n = 7;
p(arr, n);
// selection_sort(arr,n);
// p(arr,n);
// bubble_Sort(arr, n);
// p(arr, n);
insertion_sort(arr,n);
p(arr,n);
return 0;
}