Skip to content

Commit 179fb5b

Browse files
authored
Add quick sort, selection sort and stack
1 parent 232fc10 commit 179fb5b

File tree

3 files changed

+401
-0
lines changed

3 files changed

+401
-0
lines changed

src/quick_sort.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Fast and efficient method for large lists
2+
// Good for when we don't know the state of the list
3+
4+
#include <iostream>
5+
#include <string>
6+
7+
#define SIZE 10
8+
9+
// Function prototypes
10+
void printArray(int array[]);
11+
void quickSort(int array[SIZE], int start, int end);
12+
13+
/**
14+
* @brief Main function that demonstrates the sorting algorithm.
15+
*
16+
* Initializes an array of integers, prints it, sorts it using the quicksort
17+
* algorithm, and prints the sorted array.
18+
*
19+
* @return int Status code (0 for success)
20+
*/
21+
22+
int main()
23+
{
24+
// Initialize an array of integers in descending order
25+
int array[SIZE]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
26+
27+
// Print the original array
28+
printArray(array);
29+
std::cout << std::endl;
30+
std::cout << std::endl;
31+
32+
// Sort the array using quicksort
33+
quickSort(array, 0, SIZE - 1);
34+
35+
// Print the sorted array
36+
printArray(array);
37+
38+
std::cout << std::endl;
39+
system("PAUSE");
40+
return 0;
41+
}
42+
43+
/**
44+
* @brief Prints the elements of an integer array.
45+
*
46+
* This function prints each element of the array separated by a pipe ('|') for easy readability.
47+
*
48+
* @param array The integer array to be printed
49+
*/
50+
51+
void printArray(int array[])
52+
{
53+
for (int i = 0; i < SIZE; ++i)
54+
{
55+
std::cout << " |" << array[i] << "| ";
56+
}
57+
}
58+
59+
/**
60+
* @brief Sorts an array using the QuickSort algorithm.
61+
*
62+
* The QuickSort algorithm is a divide-and-conquer algorithm. It works by selecting a pivot element
63+
* from the array and partitioning the other elements into two sub-arrays, according to whether they
64+
* are smaller or greater than the pivot. The sub-arrays are then recursively sorted.
65+
*
66+
* @param array The integer array to be sorted
67+
* @param start The starting index of the section of the array to be sorted
68+
* @param end The ending index of the section of the array to be sorted
69+
*/
70+
71+
void quickSort(int array[SIZE], int start, int end)
72+
{
73+
int pivot, left, right, middle, temp;
74+
75+
// Left and right boundaries of the section being analyzed
76+
left = start;
77+
right = end;
78+
79+
// Find the middle index and set it as the pivot
80+
middle = (left + right) / 2;
81+
pivot = array[middle];
82+
83+
// Partition the array around the pivot
84+
while (right > left)
85+
{
86+
// Move the left boundary to the right until an element larger than the pivot is found
87+
while (array[left] < pivot)
88+
{
89+
left++;
90+
}
91+
92+
// Move the right boundary to the left until an element smaller than the pivot is found
93+
while (array[right] > pivot)
94+
{
95+
right--;
96+
}
97+
98+
// If the left boundary is less than or equal to the right boundary, swap the elements
99+
if (left <= right)
100+
{
101+
temp = array[left];
102+
array[left] = array[right];
103+
array[right] = temp;
104+
105+
// Move the boundaries towards the center
106+
left++;
107+
right--;
108+
}
109+
110+
// Print the array after each partition to show the sorting progress
111+
printArray(array);
112+
std::cout << std::endl;
113+
}
114+
115+
// Recursively sort the left partition if necessary
116+
if (start < right)
117+
{
118+
quickSort(array, start, right);
119+
}
120+
121+
// Recursively sort the right partition if necessary
122+
if (left < end)
123+
{
124+
quickSort(array, left, end);
125+
}
126+
}

src/selection_sort.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Good for small arrays
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
#define SIZE 10
7+
8+
// Function prototypes
9+
void printArray(int array[]);
10+
void selectionSort(int array[SIZE]);
11+
12+
/**
13+
* @brief Main function that demonstrates the sorting algorithm.
14+
*
15+
* Initializes an array of integers, prints it, sorts it using the selection sort
16+
* algorithm, and prints the sorted array.
17+
*
18+
* @return int Status code (0 for success)
19+
*/
20+
21+
int main()
22+
{
23+
// Initialize an array of integers in descending order
24+
int array[SIZE]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
25+
26+
// Print the original array
27+
printArray(array);
28+
std::cout << std::endl;
29+
30+
// Sort the array using selection sort
31+
selectionSort(array);
32+
33+
std::cout << std::endl;
34+
system("PAUSE");
35+
return 0;
36+
}
37+
38+
/**
39+
* @brief Prints the elements of an integer array.
40+
*
41+
* This function takes an array of integers and prints each element separated
42+
* by a pipe ('|') for easy readability.
43+
*
44+
* @param array The integer array to be printed
45+
*/
46+
47+
void printArray(int array[])
48+
{
49+
// Loop through each element of the array and print it with separators
50+
for (int i = 0; i < SIZE; ++i)
51+
{
52+
std::cout << " |" << array[i] << "| ";
53+
}
54+
}
55+
56+
/**
57+
* @brief Sorts an array using the Selection Sort algorithm.
58+
*
59+
* The Selection Sort algorithm works by finding the smallest element in the unsorted part
60+
* of the array and swapping it with the first unsorted element. This process is repeated
61+
* for each element until the array is sorted.
62+
*
63+
* @param array The integer array to be sorted
64+
*/
65+
66+
void selectionSort(int array[SIZE])
67+
{
68+
int minIndex, temp, i, j;
69+
70+
// Loop through each element of the array
71+
for (i = 0; i < SIZE; i++)
72+
{
73+
// Assume the current position holds the smallest element
74+
minIndex = i;
75+
76+
// Look for a smaller element in the unsorted part of the array
77+
for (j = i + 1; j < SIZE; j++)
78+
{
79+
// If a smaller element is found, update the index of the minimum element
80+
if (array[j] < array[minIndex])
81+
{
82+
minIndex = j;
83+
}
84+
}
85+
86+
// If the smallest element is not in the current position, swap them
87+
if (minIndex != i)
88+
{
89+
temp = array[i];
90+
array[i] = array[minIndex];
91+
array[minIndex] = temp;
92+
}
93+
94+
// Print the array after each iteration to show the sorting progress
95+
printArray(array);
96+
std::cout << std::endl;
97+
}
98+
}

0 commit comments

Comments
 (0)