-
Notifications
You must be signed in to change notification settings - Fork 2
/
Merge Sort Algorithm.cpp
104 lines (86 loc) · 2.62 KB
/
Merge Sort Algorithm.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
// Sorting Functions.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
void mergeSort(float *sort, int size)
{
float *Half1, *Half2; //Declare our temporary arrays
int size1, size2, Half1c = 0, Half2c = 0, sortc = 0;
if(size == 1) //Check to see if you can't spilt anymore, and begin the mergeing process
return;
size1 = size/2; size2 = size/2 + size % 2; //Set sizes, the % checks for odds. If it's three, Half1 will take 1 element and H2 will take 2
Half1 = new float[size1]; Half2 = new float[size2];
for(int count = 0; count < size1; count++)
Half1[count] = sort[count];
for(int count = size1; count < size; count++)
Half2[count - size1] = sort[count]; //The - size is so you can take the second half of size, but start at element 0 in Half
mergeSort(Half1, size1);
mergeSort(Half2, size2);
for(sortc = 0; sortc < size; sortc++) //Merge the two halfs together
{
if(Half1[Half1c] == Half2[Half2c])
{
sort[sortc] = Half1[Half1c]; Half1c++; sortc++;
sort[sortc] = Half2[Half2c]; Half2c++;
}
else if(Half1[Half1c] < Half2[Half2c])
{
sort[sortc] = Half1[Half1c]; Half1c++;
}
else
{
sort[sortc] = Half2[Half2c]; Half2c++;
}
if(Half1c >= size1 || Half2c >= size2)
break;
}
for(sortc = sortc++; sortc < size; sortc++)
{
if(Half1c >= size1 && Half2c < size2) {
sort[sortc] = Half2[Half2c]; Half2c++; }
if(Half2c >= size2 && Half1c < size1) {
sort[sortc] = Half1[Half1c]; Half1c++; }
}
delete Half1; //Clear up space!
delete Half2;
}
bool binarySearch(float *sort, int size, float target)
{
int first = 0, last = size, test;
if(size == 1 && sort[0] == target)
return true;
while(first + 1 < last)
{
test = (first + last) / 2;
if(sort[test] > target)
last = test;
else
first = test;
}
if(sort[first] == target)
return true;
else
return false;
}
int main()
{
float *hello;
int counter = 0;
std::cout << "Hello there Mr. Wiskers \n"; //Random intro
std::cout << "Enter in the amount of numbers you want sorted \n";
std::cin >> counter;
hello = new float[counter];
system("cls");
std::cout << "Now enter in the numbers \n"; //Take input
for(int count = 0; count < counter; count++) {
std::cout << "Number: ";
std::cin >> hello[count];
system("cls");
}
mergeSort(hello, counter);
for(int count = 0; count < counter; count++) //Display the sorted list
std::cout << hello[count] << ' ';
std::cout << "\n";
system("Pause");
return 0;
}