Skip to content

Commit 03e9cc0

Browse files
committed
added Vector::sort using quickSort and changed signature of sorting functions to accept an optional function pointer to be used during comparison && implemented heap sort :)
1 parent f6068e4 commit 03e9cc0

12 files changed

+110
-35
lines changed

.DS_Store

4 KB
Binary file not shown.

algs/.DS_Store

8 KB
Binary file not shown.

algs/sorting/BubbleSort.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
namespace jumbuna {
77
template<class T>
8-
void bubbleSort(T *array, size_t arraySize) {
8+
void bubbleSort(T *array, size_t arraySize, bool (*function) (T, T) = [](T i, T j) {
9+
return i > j;
10+
}) {
911
bool swapped = false;
1012
for(size_t i = 0; i < arraySize; i++) {
1113
for(size_t j = 0; j < arraySize-i; j++) {
1214
if(j+1 < arraySize) {
13-
if(array[j] > array[j+1]) {
15+
if(function(array[j], array[j+1])) {
1416
T temp = array[j];
1517
array[j] = array[j+1];
1618
array[j+1] = temp;

algs/sorting/HeapSort.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#include <algorithm>
3+
4+
namespace jumbuna {
5+
template<class T>
6+
void swap(T *array, int i, int j) {
7+
T temp = array[i];
8+
array[i] = array[j];
9+
array[j] = temp;
10+
}
11+
12+
template<class T>
13+
void heapify(T *array, int arraySize, int root, bool (*function) (T, T)) {
14+
//move small root value away from index 0
15+
int largest = root;
16+
int leftChild = 2*root+1;
17+
int rightChild = 2*root+2;
18+
if(leftChild < arraySize && function(array[leftChild], array[largest])) {
19+
largest = leftChild;
20+
}
21+
if(rightChild < arraySize && function(array[rightChild], array[largest])) {
22+
largest = rightChild;
23+
}
24+
if(largest != root) {
25+
swap(array, largest, root);
26+
heapify(array, arraySize, largest, function);
27+
}
28+
}
29+
30+
template<class T>
31+
void heapSort(T *array, int size, bool (*function) (T, T) = [](T i, T j) {
32+
return i > j;
33+
}) {
34+
//build initial max heap
35+
//size/2-1 = index of last non-leaf node
36+
for(int i =size/2-1; i >= 0; i--) {
37+
heapify(array, size, i, function);
38+
}
39+
//reduce size of heap by 1 before every loop
40+
for(int i = size-1; i >= 0; i--) {
41+
//root which is now the largest element is 'removed' from the heap
42+
swap(array, 0, i);
43+
heapify(array, i, 0, function);
44+
}
45+
}
46+
47+
}

algs/sorting/InsertionSort.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
//constant space complexity
66
namespace jumbuna {
77
template<class T>
8-
void insertionSort(T *array, size_t arraySize) {
8+
void insertionSort(T *array, size_t arraySize, bool (*function) (T, T) = [](T i, T j) {
9+
return i > j;
10+
}) {
911
for(int i = 1; i < arraySize; i++) {
10-
if(array[0] > array[i]) {
12+
if(function(array[0], array[i])) {
1113
T temp = array[i];
1214
std::memmove(array+1, array, sizeof(T)*i);
1315
array[0] = temp;
1416
}else {
1517
int newPosition = i;
1618
for(int j = i-1; j > 0; j--) {
17-
if(array[j] > array[i]) {
19+
if(function(array[j], array[i])) {
1820
newPosition = j;
21+
}else {
22+
break;
1923
}
2024
}
2125
if(newPosition != i) {

algs/sorting/QuickSort.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66

77
namespace jumbuna {
88
template<class T>
9-
void quickSort(T *array, int start, int end) {
9+
void quickSort(T *array, int start, int end, bool (*function) (T, T) = [](T i, T j) {
10+
return i > j;
11+
}) {
1012
if(start < end) {
1113
int pivot = end;
1214
for(int i=end-1; i >= start; i--) {
13-
if(array[i] > array[pivot]) {
15+
if(function(array[i], array[pivot])) {
1416
T temp = array[i];
1517
array[i] = array[pivot-1];
1618
array[pivot-1] = array[pivot];
1719
array[pivot] = temp;
1820
pivot = pivot-1;
1921
}
2022
}
21-
quickSort(array, start, pivot-1);
22-
quickSort(array, pivot+1, end);
23+
quickSort(array, start, pivot-1, function);
24+
quickSort(array, pivot+1, end, function);
2325
}
2426
}
2527
}

algs/sorting/SelectionSort.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
namespace jumbuna {
77
template<class T>
8-
void selectionSort(T *array, size_t arraySize) {
8+
void selectionSort(T *array, size_t arraySize, bool (*function) (T, T) = [](T i, T j) {
9+
return i > j;
10+
}) {
911
for(size_t i = 0; i < arraySize; i++) {
1012
size_t min = i;
11-
for(size_t j = i; j < arraySize; j++) {
12-
if(array[min] > array[j]) {
13+
for(size_t j = i+1; j < arraySize; j++) {
14+
if(function(array[min], array[j])) {
1315
min = j;
1416
}
1517
}

algs/sorting/mergeSort.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace jumbuna {
77
template<class T>
8-
void merge(T *array, size_t lsaStart, size_t lsaEnd, size_t rsaEnd) {
8+
void merge(T *array, size_t lsaStart, size_t lsaEnd, size_t rsaEnd, bool (*function) (T, T)) {
99
int ll = lsaEnd-lsaStart+1; //size of left subarray
1010
int rl = rsaEnd-lsaEnd; // size of right subarray
1111

@@ -24,7 +24,7 @@ void merge(T *array, size_t lsaStart, size_t lsaEnd, size_t rsaEnd) {
2424
//r - right subarray index
2525
int b = lsaStart, l = 0, r = 0;
2626
while(l < ll && r < rl) {
27-
if(lsa[l] <= rsa[r]) {
27+
if(!function(lsa[l], rsa[r])) {
2828
array[b] = lsa[l];
2929
++l;
3030
}else {
@@ -47,12 +47,14 @@ void merge(T *array, size_t lsaStart, size_t lsaEnd, size_t rsaEnd) {
4747
}
4848

4949
template<class T>
50-
void mergeSort(T *array, size_t start, size_t end) {
50+
void mergeSort(T *array, size_t start, size_t end, bool (*function) (T, T) = [](T i, T j) {
51+
return i >= j;
52+
}) {
5153
if(start < end) {
5254
int midian = start+(end - start)/2;
53-
mergeSort(array, start, midian);
54-
mergeSort(array, midian+1, end);
55-
merge(array, start, midian, end);
55+
mergeSort(array, start, midian, function);
56+
mergeSort(array, midian+1, end, function);
57+
merge(array, start, midian, end, function);
5658
}
5759
}
5860
}

ds/Vector.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "PoolAllocator.h"
4+
#include "../algs/sorting/QuickSort.h"
45

56
namespace jumbuna {
67
//the passed allocator is actually not used for now
@@ -45,6 +46,8 @@ class Vector {
4546
std::size_t size();
4647
//get the current capacity of the vector
4748
std::size_t currentCapacity();
49+
//sort the array
50+
void sort();
4851
//TODO implement iterators for traversal using specialized for loop
4952
};
5053
}

ds/VectorImpl.h

+5
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,9 @@ template<class T, class A>
160160
std::size_t Vector<T, A>::currentCapacity() {
161161
//get current capacity
162162
return capacity;
163+
}
164+
165+
template<class T, class A>
166+
void Vector<T, A>::sort() {
167+
jumbuna::quickSort(memoryPool->poolAddress, 0, currentIndex-1);
163168
}

test/Vector.cp

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ using namespace jumbuna;
66
int main() {
77
Vector<int> vector{4};
88
vector.push_back(10);
9-
vector.push_back(20);
9+
vector.push_back(0);
1010
vector.push_back(30);
11-
vector.push_back(40);
12-
vector.push_back(50);
13-
vector.push_back(60);
14-
vector.push_back(70);
11+
vector.push_back(99);
12+
vector.push_back(55);
13+
vector.push_back(86);
14+
vector.push_back(77);
1515
vector.push_back(80);
1616
vector.push_back(90);
17-
vector.push_back(100);
18-
vector.push_back(900);
19-
vector.push_back(6600);
20-
vector.push_back(18700);
21-
vector.push_back(10970);
17+
vector.push_back(1);
18+
vector.push_back(9);
19+
vector.push_back(600);
20+
vector.push_back(800);
21+
vector.push_back(7);
22+
23+
vector.sort();
2224

2325
for(int i = 0; i < vector.size(); i++) {
2426
std::cout << vector[i] << std::endl;
2527
}
26-
27-
std::cout << vector.currentCapacity();
2828
}

test/sorting.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
#include "../algs/sorting/InsertionSort.h"
55
#include "../algs/sorting/MergeSort.h"
66
#include "../algs/sorting/QuickSort.h"
7+
#include "../algs/sorting/HeapSort.h"
78
#include <string>
89

10+
template<class T>
11+
bool compare(T i, T j) {
12+
//descending order
13+
return i < j;
14+
}
15+
916
int main(int argc, char *argv[]) {
1017
char array[26] = {'r', 'c', 'u', 'v', 'b', 'm', 'o', 'z', 'p', 'w', 'e', 'f', 't', 'i', 'a', 'y', 's', 'd', 'g', 'h', 'j', 'l', 'n', 'q', 'k', 'x'};
1118
// for(int i = 1; i < argc; i++) {
1219
// array[i-1] = std::stoi(std::string(argv[i]));
1320
// }
14-
// jumbuna::selectionSort(array, 11);
15-
// jumbuna::bubbleSort(array, 11);
16-
// jumbuna::insertionSort(array, 11);
17-
// jumbuna::mergeSort(array, 0, 25);
18-
jumbuna::quickSort(array, 0, 25);
21+
// jumbuna::selectionSort(array, 5, ::compare);
22+
// jumbuna::bubbleSort(array, 5, ::compare);
23+
// jumbuna::insertionSort(array, 5, ::compare);
24+
// jumbuna::mergeSort(array, 0, 25, ::compare);
25+
// jumbuna::quickSort(array, 0, 25, ::compare);
26+
jumbuna::heapSort(array, 26, ::compare);
1927
for(int i = 0; i < 26; i++) {
2028
std::cout << array[i] << ", ";
2129
}

0 commit comments

Comments
 (0)