-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort.cpp
More file actions
61 lines (56 loc) · 1.1 KB
/
Selection_Sort.cpp
File metadata and controls
61 lines (56 loc) · 1.1 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
///Time Complexity: O(n^2) for the worst case
//Time Complexity: O(n) for the best case
/*
Auther : Abdullah Al Masum
*/
#include <bits/stdc++.h>
using namespace std;
#define MAXX 10000000
int Arr[MAXX];
void printArray(int A[], int N)
{
for (int i = 0; i < N; i++)
{
cout << A[i] << " ";
}
cout << endl;
}
void swapp(int *A, int i, int j)
{
int temp = A[j];
A[j] = A[i];
A[i] = temp;
}
void SelectionSort(int A[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (A[j] < A[minIndex])
{
minIndex = j;
}
}
swapp(A, minIndex, i);
cout << "Working on pass number " << i + 1 << " : ";
printArray(A, n);
}
}
int main()
{
//takeInput();
int size;
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> Arr[i];
}
cout << "Before Sorting: " << endl;
printArray(Arr, size);
SelectionSort(Arr, size);
cout << "After Sorting:" << endl;
printArray(Arr, size);
return 0;
}