forked from Ansi007/Programming-Fundamentals-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic sorts.cpp
105 lines (94 loc) · 2.09 KB
/
basic sorts.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
102
103
104
105
#include "iostream"
#include "fstream"
#include "ctime"
using namespace std;
//Add necessary instructions to count the Number of Key Comparsisons and Data Movements
//You are NOT Allowed to add any input/output instructions in sorting algorithms
//Bubble Sort
int count1 =0;
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
count1++;
}
int bubbleSort(int a[], int size)
{
int count = 0;
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (++count && a[j] > a[j + 1])
swap(a[j], a[j + 1]);
}
}
return count;
}
//Selection Sort
void selectionSort(int a[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int min = i;
for (int j = i + 1; j < size; j++)
{
if (a[min] > a[j])
min = j;
}
swap(a[i], a[min]);
}
}
//Insertion Sort
void insertionSort(int a[], int size)
{
int j = 0;
for (int i = 0; i < size - 1; i++)
{
j = i + 1;
while (j > 0 && a[j] < a[j - 1])
{
swap(a[j], a[j - 1]);
j--;
}
}
}
int main()
{
//Read the data from file here
ifstream file("in00.txt");
int size;
file >> size;
int *arr = new int[size];
/* int *arr1 = new int[size];
int *arr2 = new int[size];
int *arr3 = new int[size];
int *arr4 = new int[size];
int *arr5 = new int[size];*/
for (int i = 0; i < size; i++){
file >> arr[i];
// arr1[i] = arr2[i] = arr3[i] = arr4[i] = arr5[i];
}
clock_t startTime = clock();
//Make a call to sorting algorithm. DO NOT perform any other operation here
int c = bubbleSort(arr, size);
/* bubbleSort(arr1, size);
bubbleSort(arr2, size);
bubbleSort(arr3, size);
bubbleSort(arr4, size);
bubbleSort(arr5, size);*/
/* insertionSort(arr,size);
insertionSort(arr1,size);
insertionSort(arr2,size);
insertionSort(arr3,size);
insertionSort(arr4,size);
insertionSort(arr5,size);*/
clock_t endTime = clock();
double elapse = ((double)endTime - (double)startTime) / CLOCKS_PER_SEC;
// cout << "The operation took " << elapse/6.0 << " seconds";
//int c=bubbleSort(arr, size);
//Add any other instructions here, if required
cout<<"no of key comp "<<c<<endl;
//cout<<"swap wala:"<<count1<<endl;
return 0;
}