Skip to content

Commit

Permalink
Added sort algorithms. Bubble sort, Bucket sort, Heap sort, Insertion…
Browse files Browse the repository at this point in the history
… 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
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(DataStructures_CPP)

set(CMAKE_CXX_STANDARD 17)

add_library(DataStructures_CPP src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h src/Array/Queue.cpp src/Array/Queue.h src/List/Queue.cpp src/List/Queue.h src/Tree/Tree.cpp src/Tree/Tree.h src/Tree/TreeNode.cpp src/Tree/TreeNode.h src/Tree/Element.cpp src/Tree/Element.h src/Tree/Stack.cpp src/Tree/Stack.h src/Tree/Queue.cpp src/Tree/Queue.h src/Tree/AvlTree.cpp src/Tree/AvlTree.h src/Tree/AvlTreeNode.cpp src/Tree/AvlTreeNode.h src/Tree/BTree.cpp src/Tree/BTree.h src/Tree/BTreeNode.cpp src/Tree/BTreeNode.h src/Array/Hash.cpp src/Array/Hash.h src/List/Hash.cpp src/List/Hash.h src/Array/Heap/Heap.cpp src/Array/Heap/Heap.h src/Array/Heap/HeapNode.cpp src/Array/Heap/HeapNode.h src/Array/Heap/DHeap.cpp src/Array/Heap/DHeap.h src/Array/Set.cpp src/Array/Set.h src/Array/DisjointSet.cpp src/Array/DisjointSet.h src/Array/Graph/Graph.cpp src/Array/Graph/Graph.h src/List/Graph/Edge.cpp src/List/Graph/Edge.h src/List/Graph/EdgeList.cpp src/List/Graph/EdgeList.h src/List/Graph/Graph.cpp src/List/Graph/Graph.h src/General/Path.cpp src/General/Path.h src/General/AbstractGraph.cpp src/General/AbstractGraph.h src/Array/Heap/MaxHeap.cpp src/Array/Heap/MaxHeap.h src/Array/Heap/MaxDHeap.cpp src/Array/Heap/MaxDHeap.h src/Array/Heap/MinHeap.cpp src/Array/Heap/MinHeap.h src/Array/Heap/MinDHeap.cpp src/Array/Heap/MinDHeap.h)
add_library(DataStructures_CPP src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h src/Array/Queue.cpp src/Array/Queue.h src/List/Queue.cpp src/List/Queue.h src/Tree/Tree.cpp src/Tree/Tree.h src/Tree/TreeNode.cpp src/Tree/TreeNode.h src/Tree/Element.cpp src/Tree/Element.h src/Tree/Stack.cpp src/Tree/Stack.h src/Tree/Queue.cpp src/Tree/Queue.h src/Tree/AvlTree.cpp src/Tree/AvlTree.h src/Tree/AvlTreeNode.cpp src/Tree/AvlTreeNode.h src/Tree/BTree.cpp src/Tree/BTree.h src/Tree/BTreeNode.cpp src/Tree/BTreeNode.h src/Array/Hash.cpp src/Array/Hash.h src/List/Hash.cpp src/List/Hash.h src/Array/Heap/Heap.cpp src/Array/Heap/Heap.h src/Array/Heap/HeapNode.cpp src/Array/Heap/HeapNode.h src/Array/Heap/DHeap.cpp src/Array/Heap/DHeap.h src/Array/Set.cpp src/Array/Set.h src/Array/DisjointSet.cpp src/Array/DisjointSet.h src/Array/Graph/Graph.cpp src/Array/Graph/Graph.h src/List/Graph/Edge.cpp src/List/Graph/Edge.h src/List/Graph/EdgeList.cpp src/List/Graph/EdgeList.h src/List/Graph/Graph.cpp src/List/Graph/Graph.h src/General/Path.cpp src/General/Path.h src/General/AbstractGraph.cpp src/General/AbstractGraph.h src/Array/Heap/MaxHeap.cpp src/Array/Heap/MaxHeap.h src/Array/Heap/MaxDHeap.cpp src/Array/Heap/MaxDHeap.h src/Array/Heap/MinHeap.cpp src/Array/Heap/MinHeap.h src/Array/Heap/MinDHeap.cpp src/Array/Heap/MinDHeap.h src/Array/Sort/Sort.h src/Array/Sort/BubbleSort.cpp src/Array/Sort/BubbleSort.h src/Array/Sort/BucketSort.cpp src/Array/Sort/BucketSort.h src/Array/Sort/HeapSort.cpp src/Array/Sort/HeapSort.h src/Array/Sort/InsertionSort.cpp src/Array/Sort/InsertionSort.h src/Array/Sort/MergeSort.cpp src/Array/Sort/MergeSort.h src/Array/Sort/QuickSort.cpp src/Array/Sort/QuickSort.h src/Array/Sort/SelectionSort.cpp src/Array/Sort/SelectionSort.h src/Array/Sort/ShellSort.cpp src/Array/Sort/ShellSort.h)
add_executable(TestLinkedList src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h Test/LinkedListTest.cpp src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h)
add_executable(MathematicalExpression src/Array/MathematicalExpression/TokenType.h src/Array/MathematicalExpression/Token.cpp src/Array/MathematicalExpression/Token.h src/Array/MathematicalExpression/Element.h src/Array/MathematicalExpression/Element.cpp src/Array/MathematicalExpression/Stack.h src/Array/MathematicalExpression/Stack.cpp src/Array/MathematicalExpression/Evaluate.cpp)
add_executable(Polynomial src/List/Polynomial/TestPolynomial.cpp src/List/Polynomial/Term.cpp src/List/Polynomial/Term.h src/List/Polynomial/TermNode.cpp src/List/Polynomial/TermNode.h src/List/Polynomial/Polynomial.cpp src/List/Polynomial/Polynomial.h)
Expand Down
20 changes: 20 additions & 0 deletions src/Array/Sort/BubbleSort.cpp
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;
}
}
}
}
17 changes: 17 additions & 0 deletions src/Array/Sort/BubbleSort.h
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
29 changes: 29 additions & 0 deletions src/Array/Sort/BucketSort.cpp
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;
}
20 changes: 20 additions & 0 deletions src/Array/Sort/BucketSort.h
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
18 changes: 18 additions & 0 deletions src/Array/Sort/HeapSort.cpp
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();
}
}
17 changes: 17 additions & 0 deletions src/Array/Sort/HeapSort.h
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
17 changes: 17 additions & 0 deletions src/Array/Sort/InsertionSort.cpp
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;
}
}
17 changes: 17 additions & 0 deletions src/Array/Sort/InsertionSort.h
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
46 changes: 46 additions & 0 deletions src/Array/Sort/MergeSort.cpp
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);
}
20 changes: 20 additions & 0 deletions src/Array/Sort/MergeSort.h
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
34 changes: 34 additions & 0 deletions src/Array/Sort/QuickSort.cpp
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);
}
20 changes: 20 additions & 0 deletions src/Array/Sort/QuickSort.h
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
22 changes: 22 additions & 0 deletions src/Array/Sort/SelectionSort.cpp
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;
}
}
}
17 changes: 17 additions & 0 deletions src/Array/Sort/SelectionSort.h
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
25 changes: 25 additions & 0 deletions src/Array/Sort/ShellSort.cpp
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;
}
}
}
21 changes: 21 additions & 0 deletions src/Array/Sort/ShellSort.h
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
14 changes: 14 additions & 0 deletions src/Array/Sort/Sort.h
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

0 comments on commit 3bc2fa2

Please sign in to comment.