-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
61 lines (52 loc) · 1.66 KB
/
Copy pathBubbleSort.cpp
File metadata and controls
61 lines (52 loc) · 1.66 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
/*
Julio Aguilar
Bubble Sort
Cs942 class project
*/
#include<iostream>
using namespace std;
//prototypes
void sort(int values[], int m);
int main()
{
//Declorations
int nums[] = {41, 39, 43, 40, 52, 63, 88, 32, 54, 66, 12, 16, 5, 8}; //array of integers(int size must equal the amount of elements)
int size = 14; //Size of array (needs to be adjusted whenever the number of elements in array nums[] is changed)
//Starts sort
sort(nums, size);
//Prints Sorted Array of nums[]
for (int i = 0; i < size; i++)
{
if (i < (size - 1))
cout << nums[i] << " \n";
} cout << endl;
//Return Success
system("pause");
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
//Fucntion Prototype(s)
//////////////////////////////////////////////////////////////////////////////////////////
//Function to sort
void sort(int values[], int s)
{
//Temp Declorations
int counter; //used to determine loop restart
int holder; //value holder when when swapping arrays elements
do //Sort
{
//initialize counter here to allow loop entrance
counter = 0; //resets to 0 when condition fails to exit loop
//start sort loop
for (int i = 0; i < (s - 1); i++)
{
if (values[i] > values[i + 1])
{
holder = values[i]; //////////
values[i] = values[i + 1]; //Swapping
values[i + 1] = holder; //////////
counter++; //prevents loop exit if two elements are swapped
}
}
} while (counter != 0); //condition required to exit loop
}