-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sort algorithms. Bubble sort, Bucket sort, Heap sort, Insertion…
… sort, Merge sort, Quick sort, Selection sort, and Shell sort.
- Loading branch information
Olcay Taner YILDIZ
committed
May 27, 2023
1 parent
10c0d9e
commit 3bc2fa2
Showing
18 changed files
with
375 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "BubbleSort.h" | ||
|
||
void BubbleSort::sort(int *A, int size) { | ||
bool exchanged = true; | ||
while (exchanged){ | ||
exchanged = false; | ||
for (int i = 0; i < size - 1; i++){ | ||
if (A[i] > A[i + 1]){ | ||
exchanged = true; | ||
int tmp = A[i]; | ||
A[i] = A[i + 1]; | ||
A[i + 1] = tmp; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_BUBBLESORT_H | ||
#define DATASTRUCTURES_CPP_BUBBLESORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class BubbleSort : public Sort{ | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_BUBBLESORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "BucketSort.h" | ||
|
||
BucketSort::BucketSort(int maxValue) { | ||
this->maxValue = maxValue; | ||
} | ||
|
||
void BucketSort::sort(int *A, int size) { | ||
int* C = new int[maxValue]; | ||
int* B = new int[size]; | ||
for (int i = 0; i < size; i++){ | ||
C[A[i]]++; | ||
} | ||
for (int i = 1; i < maxValue; i++){ | ||
C[i] += C[i - 1]; | ||
} | ||
for (int i = size - 1; i >= 0; i--){ | ||
B[C[A[i]] - 1] = A[i]; | ||
C[A[i]]--; | ||
} | ||
for (int i = 0; i < size; i++){ | ||
A[i] = B[i]; | ||
} | ||
delete[] C; | ||
delete[] B; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_BUCKETSORT_H | ||
#define DATASTRUCTURES_CPP_BUCKETSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class BucketSort : public Sort{ | ||
private: | ||
int maxValue; | ||
public: | ||
explicit BucketSort(int maxValue); | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_BUCKETSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "HeapSort.h" | ||
#include "../Heap/MinHeap.h" | ||
|
||
void HeapSort::sort(int *A, int size) { | ||
MinHeap heap = MinHeap(size); | ||
for (int i = 0; i < size; i++){ | ||
HeapNode heapNode = HeapNode(A[i], i); | ||
heap.insert(heapNode); | ||
} | ||
for (int i = 0; i < size; i++){ | ||
HeapNode heapNode = heap.deleteTop(); | ||
A[i] = heapNode.getData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_HEAPSORT_H | ||
#define DATASTRUCTURES_CPP_HEAPSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class HeapSort : public Sort{ | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_HEAPSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "InsertionSort.h" | ||
|
||
void InsertionSort::sort(int *A, int size) { | ||
for (int j = 1; j < size; j++){ | ||
int t = A[j]; | ||
int i = j - 1; | ||
while (i >= 0 && A[i] > t){ | ||
A[i + 1] = A[i]; | ||
i--; | ||
} | ||
A[i + 1] = t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_INSERTIONSORT_H | ||
#define DATASTRUCTURES_CPP_INSERTIONSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class InsertionSort : public Sort{ | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_INSERTIONSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include <climits> | ||
#include "MergeSort.h" | ||
|
||
void MergeSort::merge(int *A, int start, int middle, int end) { | ||
int leftCount = middle - start + 1; | ||
int rightCount = end - middle; | ||
int* leftPart = new int[leftCount + 1]; | ||
int* rightPart = new int[rightCount + 1]; | ||
for (int i = 0; i < leftCount; i++){ | ||
leftPart[i] = A[start + i]; | ||
} | ||
for (int i = 0; i < rightCount; i++){ | ||
rightPart[i] = A[middle + i]; | ||
} | ||
leftPart[leftCount] = INT_MAX; | ||
rightPart[rightCount] = INT_MAX; | ||
int i = 0, j = 0; | ||
for (int k = start; k <= end; k++){ | ||
if (leftPart[i] <= rightPart[j]){ | ||
A[k] = leftPart[i]; | ||
i++; | ||
} else { | ||
A[k] = rightPart[j]; | ||
j++; | ||
} | ||
} | ||
delete[] leftPart; | ||
delete[] rightPart; | ||
} | ||
|
||
void MergeSort::mergeSort(int *A, int first, int last) { | ||
if (first < last){ | ||
int pivot = (first + last) / 2; | ||
mergeSort(A, first, pivot); | ||
mergeSort(A, pivot + 1, last); | ||
merge(A, first, pivot, last); | ||
} | ||
} | ||
|
||
void MergeSort::sort(int *A, int size) { | ||
mergeSort(A, 0, size - 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_MERGESORT_H | ||
#define DATASTRUCTURES_CPP_MERGESORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class MergeSort : public Sort{ | ||
private: | ||
void merge(int* A, int start, int middle, int end); | ||
void mergeSort(int* A, int first, int last); | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_MERGESORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "QuickSort.h" | ||
|
||
int QuickSort::partition(int* A, int first, int last) { | ||
int x = A[last]; | ||
int i = first - 1; | ||
for (int j = first; j < last; j++){ | ||
if (A[j] <= x){ | ||
i++; | ||
int tmp = A[i]; | ||
A[i] = A[j]; | ||
A[j] = tmp; | ||
} | ||
} | ||
int tmp = A[i + 1]; | ||
A[i + 1] = A[last]; | ||
A[last] = tmp; | ||
return i + 1; | ||
} | ||
|
||
void QuickSort::quickSort(int *A, int first, int last) { | ||
if (first < last){ | ||
int pivot = partition(A, first, last); | ||
quickSort(A, first, pivot - 1); | ||
quickSort(A, pivot + 1, last); | ||
} | ||
} | ||
|
||
void QuickSort::sort(int *A, int size) { | ||
quickSort(A, 0, size - 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_QUICKSORT_H | ||
#define DATASTRUCTURES_CPP_QUICKSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class QuickSort : public Sort{ | ||
private: | ||
int partition(int* A, int first, int last); | ||
void quickSort(int* A, int first, int last); | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_QUICKSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "SelectionSort.h" | ||
|
||
void SelectionSort::sort(int *A, int size) { | ||
for (int i = 0; i < size - 1; i++){ | ||
int min = A[i]; | ||
int pos = i; | ||
for (int j = i + 1; j < size; j++){ | ||
if (A[j]< min){ | ||
min = A[j]; | ||
pos = j; | ||
} | ||
} | ||
if (pos != i){ | ||
A[pos] = A[i]; | ||
A[i] = min; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_SELECTIONSORT_H | ||
#define DATASTRUCTURES_CPP_SELECTIONSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class SelectionSort : public Sort{ | ||
public: | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_SELECTIONSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#include "ShellSort.h" | ||
|
||
ShellSort::ShellSort(int *H, int incrementSize) { | ||
this->H = H; | ||
this->incrementSize = incrementSize; | ||
} | ||
|
||
void ShellSort::sort(int *A, int size) { | ||
for (int k = 0; k < incrementSize; k++){ | ||
int increment = H[k]; | ||
for (int j = increment; j < size; j++){ | ||
int t = A[j]; | ||
int i = j - increment; | ||
while (i >= 0 && A[i] > t){ | ||
A[i + increment] = A[i]; | ||
i -= increment; | ||
} | ||
A[i + increment] = t; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_SHELLSORT_H | ||
#define DATASTRUCTURES_CPP_SHELLSORT_H | ||
|
||
|
||
#include "Sort.h" | ||
|
||
class ShellSort : public Sort{ | ||
private: | ||
int* H; | ||
int incrementSize; | ||
public: | ||
ShellSort(int* H, int incrementSize); | ||
void sort(int* A, int size) override; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_SHELLSORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Created by Olcay Taner YILDIZ on 27.05.2023. | ||
// | ||
|
||
#ifndef DATASTRUCTURES_CPP_SORT_H | ||
#define DATASTRUCTURES_CPP_SORT_H | ||
|
||
|
||
class Sort { | ||
virtual void sort(int* A, int size) = 0; | ||
}; | ||
|
||
|
||
#endif //DATASTRUCTURES_CPP_SORT_H |