forked from wafarifki/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection Sort Visualised.cpp
70 lines (61 loc) · 1.13 KB
/
Selection Sort Visualised.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
/*
Author: Siddhant Pandy
Program: Graphical representation of Sorting in command line
Topic: Selection Sort
*/
#include<iostream>
#include<vector>
#include <time.h>
using namespace std;
void delay(int number_of_seconds)
{
int milli_seconds = 1000 * number_of_seconds;
clock_t start_time = clock();
while (clock() < start_time + milli_seconds)
;
}
void display(vector <int> arr)
{
for(int x=0;x<arr.size();x++)
{
cout<<"|"<<x<<"|";
for(int y=0;y<arr[x];y++)
{
cout<<"-";
}
cout<<arr[x]<<endl;
}
cout<<endl<<endl;
delay(2);
}
void swap(int *xp, int *yp)
{
int temp=*xp;
*xp=*yp;
*yp=temp;
}
vector<int> selectionSort(vector<int> arr)
{
for(int x=0;x<arr.size()-1;x++)
{
int min=x;
for(int y=x+1;y<arr.size();y++)
{
if(arr[min]>arr[y])
{
min=y;
}
}
swap(arr[min],arr[x]);
display(arr);
}
return arr;
}
int main()
{
//int arr[] = {10,3,6,9,1,5,2,4,8,7};
int arr[] = {54, 89, 62, 26, 78,51};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));
vec=selectionSort(vec);
display(vec);
}