⛄ Currently frozen ⛄
If you want to learn something, try to teach it
This repository has been created to store all the tutorial projects I've written. I will try to place each tutorial project in C and C++. All these projects consist of search and sorting algorithms, which are the basis for other more complex projects. Also, these projects consist Data Structures.
It's a great opportunity to learn Git, Make for build automation and Markdown for readme and just help beginners learn C/C++.
Note: Use Table of content in Readme
Note: Use make in ./ to make
make | Content |
---|---|
make c_search |
searching algorithms in C |
make cpp_search |
searching algrotithms in C++ |
make search |
searching algorithms in C and C++ |
make c_sort |
sorting algorithms in C (currently not working) |
make cpp_sort |
sorting algorithms in C++ (currently not working) |
make sort |
sorting algorithms in C and C++ (currently not working) |
make all |
searching and sorting algorithms in C and C++ (currently not working) |
make clean |
delete created executable and objective files |
- ncurses
- sorting algorithms
- data structures
- style check
- testing
- in-place merge sort testing
- Linear search
- Binary search
- Quicksort
- Merge sort
- In-place merge sort
- Introsort
- Heapsort
- Insertion sort
- Block sort
- Timsort
- Selection sort
- Cubesort
- Shellsort
- Bubble sort
- Exchange sort
- Tree sort
- Cycle sort
- Library sort
- Patience sorting
- Smoothsort
- Strand sort
- Tournament sort
- Cocktail shaker sort
- Comb sort
- Gnome sort
- Odd-even sort
- Array
- String
- List
- Vector
- Queue
- Stack
- Set
- Binary Tree
- Graph
A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm reaches the end of the list, the search terminates unsuccessfully.
Perfromance | Big O |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- Start at the beginning.
- Compare the first element with the target.
- If the current element matches the target value, the search is successful.
- If the current element does not match the target value, move to the next element.
- Repeat steps 3 and 4.
- If you reach the end of the list without finding a match, the target value is not present in the list or array.
Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.
Perfromance | Big O |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- Start with a sorted array of elements. Binary search works efficiently on sorted arrays.
- Set two pointers, "low" and "high," to mark the lower and upper bounds of the search space. Initially, "low" points to the first element, and "high" points to the last element of the array.
- Calculate the middle index of the search space by taking the average of "low" and "high":
$middle = \frac{low + high}{2}$ - Compare the target value you're searching for with the element at the middle index:
- If the target value matches the element at the middle index, the search is successful, and you can return the index or any desired output.
- If the target value is less than the element at the middle index, it means the target must be in the lower half of the search space. Update "high" to be one less than the middle index:
$high = middle - 1$ - If the target value is greater than the element at the middle index, it means the target must be in the upper half of the search space. Update "low" to be one more than the middle index:
$low = middle + 1$
- Repeat steps 3-4 until either the target value is found or the search space is empty. An empty search space occurs when "low" becomes greater than "high".
- If the search space is empty and the target value has not been found, it means the target is not present in the array.
Quicksort (Tony Hoare) is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.
Perfromance | Big O |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- Choose a pivot element from the array. This pivot element will be used to partition the array into two halves.
- Reorder the array so that all elements smaller than the pivot come before the pivot, and all elements greater than the pivot come after it. This process is called partitioning. After this step, the pivot element will be in its final sorted position.
- Recursively apply the above steps to the sub-array of elements that are smaller than the pivot and the sub-array of elements that are greater than the pivot.
- Repeat steps 1 to 3 until the entire array is sorted. This means recursively applying the algorithm to the smaller sub-arrays until they contain only one element (which is already considered sorted).
Merge Sort is a popular sorting algorithm that follows the divide-and-conquer principle. It works by dividing the input array into smaller sub-arrays, sorting them separately, and then merging them back together to obtain a sorted array.
Perfromance | Big O |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- Divide. The input array is recursively divided into two halves until we reach the base case, which is when the array contains only one element or is empty.
- Conquer. Once the array is divided into individual elements, the merging process begins. The two sub-arrays are merged by comparing the elements at corresponding positions and placing them in the correct order. This is done recursively until all sub-arrays are merged.
- Merge. To merge the sub-arrays, we create an auxiliary array to store the sorted elements. We maintain three pointers:
i
for the left sub-array,j
for the right sub-array, andk
for the auxiliary array. At each step, we compare the elements at positionsi
andj
. The smaller element is placed in the auxiliary array at positionk
, and the corresponding pointer is incremented. This process continues until all elements have been merged. - Copy. After merging all the sub-arrays, the sorted elements are stored in the auxiliary array. We copy these elements back into the original input array, replacing the unsorted elements with the sorted ones.
In-place merge sort is an efficient sorting algorithm that works by repeatedly dividing the input array into smaller subarrays, sorting them, and then merging them back together in-place. It is called "in-place" because it does not require extra memory to perform the sorting process. The main idea behind the in-place merge sort is to use a combination of merging techniques and recursion to sort the array. Here are the key steps involved in the algorithm:
- Divide the input array into two halves by finding the middle index.
- Recursively apply the merge sort algorithm to both halves.
- Merge the two sorted subarrays back into the original array using an in-place merging technique.
Perfromance | Big O |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- If the input array has only one element or is empty, it is already considered sorted.
- Divide the array into two halves by finding the middle index.
- Recursively apply the in-place merge sort algorithm to the first half of the array.
- Recursively apply the in-place merge sort algorithm to the second half of the array.
- Merge the two sorted halves back into the original array using the in-place merging technique.
- Repeat steps 2-5 until the entire array is sorted.
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass-throughs continue until the list is sorted.
Perfromance | Big O comprasions |
---|---|
Worst-case | |
Best-case | |
Average |
Perfromance | Big O swaps |
---|---|
Worst-case | |
Best-case | |
Average |
Algorithm
- Start at the begin of the list
- Compare element and next from it
- If the current element is greater than the next element, swap them.
- Go to next element and repeat third step.
- If th current element - last element of the list, list is sorted.
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
In progress...
Algorithm
In progress...
Array - collection of elements. All elements of the array have the same memory size. They have at least one array index or key .
Example (One-dimensional array of 10 ints):
void foo{
int N = 10; // Size of array
int Array_A[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // elements of array
int Array_B[10]; // empty array (size 10)
}
Also, array elements can be arrays themselves. This allows for creating a two-dimensional array. This structure resembles a chessboard or a table. Moreover, the size of the array and the size of the array-elements can differ. However, the size of all array-elements must match by definition of an array.
Example (Two-dimensional array of 10x10 ints):
void foo{
int N = 10; // Size of array
int M = 10; // Size of array-elements
int Array_A[N][M]; // Two-dimensional array
}
Of course, it is evident that both three-dimensional and n-dimensional arrays can be created. Are arrays in C++ same as C?
In progress...
In progress...
In progress...
In progress...
In progress...
In progress...
In progress...
In progress...