-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbshirMohamedQuiz27.cpp
149 lines (109 loc) · 3.54 KB
/
AbshirMohamedQuiz27.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Chapter 8, Programming Challenge: Sorting Benchmarks
#include <iostream>
using namespace std;
// Constant for array sizes
const int SIZE = 20;
// Function prototypes
void bubbleSort(long[],int,int&);
void selectionSort(long[],int,int&);
void showValues(long[],int);
void swap(int &, int &);
int main()
{
int exchanges; // Number of exchanges made
// Two arrays with identical values
long accounts1[SIZE] =
{ 5658845, 4520125, 7895122,
8777541, 8451277, 1302850,
8080152, 4562555, 5552012,
5050552, 7825877, 1250255,
1005231, 6545231, 3852085,
7576651, 7881200, 4581002 };
long accounts2[SIZE] =
{ 5658845, 4520125, 7895122,
8777541, 8451277, 1302850,
8080152, 4562555, 5552012,
5050552, 7825877, 1250255,
1005231, 6545231, 3852085,
7576651, 7881200, 4581002 };
// Sort accounts1 with bubble sort. The function
// returns the number of exchanges made.
// write your code here
bubbleSort(accounts1,SIZE,exchanges);
// Display the number of exchanges made by the
// bubble sort.
cout << "\n" << exchanges
<< " exchanges were made by Bubble Sort."
<< endl;
exchanges = 0;
// Sort accounts2 with selection sort. The function
// returns the number of exchanges made.
// write your code here
selectionSort(accounts2,SIZE,exchanges);
cout << "\n" << exchanges
<< " exchanges were made by Selection Sort."
<< endl;
//Call the showValue function twice to see your sorting result
cout<<"***************************************"<<endl;
showValues(accounts1,SIZE);
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
showValues(accounts2,SIZE);
cout<<"***************************************"<<endl;
return 0;
}
// ********************************************************
// The bubbleSort function performs the bubble sort on *
// the array and returns the number of exchanges made. *
// ********************************************************
void bubbleSort(long array[], int size,int& exchange)
{
int maxElement;
int index;
for (maxElement = size - 1; maxElement > 0; maxElement--)
{
for (index = 0; index < maxElement; index++)
{
if (array[index] > array[index + 1])
{
exchange++;
swap(array[index], array[index + 1]);
}
}
}
}
// ********************************************************
// The selectionSort function performs the selection sort *
// on array and returns the number of exchanges made. *
// ********************************************************
void selectionSort(long array[], int size,int& exchange)
{
int minIndex, minValue;
for (int start = 0; start < (size - 1); start++)
{
minIndex = start;
minValue = array[start];
for (int index = start + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
swap(array[minIndex], array[start]);
exchange++;
}
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
//Define the showValue function
void showValues(long arr[],int size)
{
for(int i = 0;i<(size-1);i++)
cout<<arr[i]<<endl;
}