-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
88 lines (69 loc) · 1.78 KB
/
BubbleSort.cpp
File metadata and controls
88 lines (69 loc) · 1.78 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
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
#include <iostream>
using namespace std;
void getArrayFromUser (int* startingAddress , int* endingAddress)
{
while(startingAddress != endingAddress)
{
cout << "Enter the element : ";
cin >> *startingAddress;
startingAddress ++;
}
cout << endl;
}
void allocateArray (int* & myArray , int &size)
{
cout << "Enter the size of the Array : ";
cin >> size;
cout << endl;
while (size <= 0)
{
cout << "The size , that you entered , is not valid . " <<endl;
cout << "Enter a valid size : ";
cin >> size;
}
myArray = new int[size];
}
void swapByPointers (int* a , int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort (int* startingAddress , int* endingAddress)
{
int* endPtr = endingAddress;
for (int* startPtr = startingAddress ; startPtr != endPtr; startPtr++)
{
for (int* i = startingAddress ; i != (endPtr - 1) ; i++)
{
if (*i >= *(i + 1))
{
swapByPointers (i , (i + 1));
}
}
}
}
void displaySortedArray (int* startingAddress , int* endingAddress)
{
for (int* i = startingAddress ; i != endingAddress ; i++ )
cout <<"Element of the sorted array : " << *i << endl;
cout << endl;
}
void deAllocateArray (int* myArray)
{
delete [] myArray ;
cout << "The array has successfully been de-allocated .";
}
int main ()
{
int* myArray = NULL;
int size = 0;
allocateArray(myArray , size);
int* startingAddress = myArray;
int* endingAddress = (myArray + size);
getArrayFromUser (startingAddress , endingAddress);
bubbleSort (startingAddress , endingAddress);
displaySortedArray (startingAddress , endingAddress);
deAllocateArray (myArray);
return 0;
}