Repository navigation
Expand file tree
/
Copy pathsorting.h
More file actions
86 lines (72 loc) · 2.55 KB
/
Copy pathsorting.h
File metadata and controls
86 lines (72 loc) · 2.55 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Sorting{
public:
static void MergeSort(int *arr,int len){
//base case
if(len <= 1)
return;
//split array
int fSize = (len/2),sSize = (len - fSize),first[fSize],second[sSize],Sorted[len],fIndex = 0,sIndex = 0;
memcpy(first,arr,fSize*sizeof(int));
memcpy(second,arr+fSize,sSize*sizeof(int));
//sort
MergeSort(first,fSize);
MergeSort(second,sSize);
//sort sorted arrays
while((fIndex+sIndex) != len){
if((fIndex != fSize && first[fIndex] <= second[sIndex]) || sIndex == sSize){
Sorted[(fIndex+sIndex)] = first[fIndex];
fIndex++;
}else{
Sorted[(fIndex+sIndex)] = second[sIndex];
sIndex++;
}
}
//return
memcpy(arr,Sorted,len*sizeof(int));
}
static void QuickSort(int *arr,int len){
//base case
if(len <= 1)
return;
//delcare ints
int pivot = arr[0],smaller[len],larger[len],largerIndex = 0,smallerIndex = 0;
//sort based on pivot
for(int index = 1; index != len;index++)
{
if(arr[index] < pivot){
smaller[smallerIndex] = arr[index];
smallerIndex++;
}else{
larger[largerIndex] = arr[index];
largerIndex++;
}
}
//sort
QuickSort(larger,largerIndex);
QuickSort(smaller,smallerIndex);
//reassemble
memcpy(arr,smaller,smallerIndex*sizeof(int));
arr[smallerIndex] = pivot;
memcpy(arr+smallerIndex+1,larger,largerIndex*sizeof(int));
}
int static BinarySearch(int *arr,int len,int num){
//base case
if(arr[len/2] == num)
return(len/2);
if(len <= 1){
return -1;
}
int half[(len/2)+1];
if(arr[len/2] > num){
memcpy(half,arr,(len/2)*sizeof(int));
return BinarySearch(half,(len/2),num);
}else{
memcpy(half,arr+(len/2)+1,(len-(len/2))*sizeof(int));
int out = BinarySearch(half,(len-(len/2)),num);
return ((out != -1)* (1+(len/2))) + out;
}
}
};