Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
Sorting Algorithm

Files

file  merge_sort.cpp
 Merege Sort Algorithm (MEREGE SORT) implementation
 
file  wiggle_sort.cpp
 [Wiggle Sort Algorithm] (https://leetcode.com/problems/wiggle-sort-ii/) Implementation
 

Namespaces

namespace  sorting
 for working with vectors
 

Functions

template<typename T >
void heapify (T *arr, int n, int i)
 
template<typename T >
void heapSort (T *arr, int n)
 
void merge (int *arr, int l, int m, int r)
 
void mergeSort (int *arr, int l, int r)
 
void show (int *arr, int size)
 
int main ()
 
template<typename T >
static void displayElements (const std::vector< T > &arr)
 Utility function used for printing the elements. Prints elements of the array after they're sorted using wiggle sort algorithm.
 
static void test ()
 

Detailed Description

The heapify procedure can be thought of as building a heap from the bottom up by successively sifting downward to establish the heap property.

Parameters
arrarray to be sorted
nsize of array
inode position in Binary Tress or element position in Array to be compared with it's childern

Function Documentation

◆ displayElements()

template<typename T >
static void displayElements ( const std::vector< T > & arr)
static

Utility function used for printing the elements. Prints elements of the array after they're sorted using wiggle sort algorithm.

Parameters
arrarray containing the sorted elements
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.
85 {
86 uint32_t size = arr.size();
87
88 std::cout << "Sorted elements are as follows: ";
89
90 std::cout << "[";
91
92 for (int i = 0; i < size; i++) {
93 std::cout << arr[i];
94 if (i != size - 1) {
95 std::cout << ", ";
96 }
97 }
98
99 std::cout << "]" << std::endl;
100}
T endl(T... args)
T size(T... args)
Here is the call graph for this function:

◆ heapify()

template<typename T >
void heapify ( T * arr,
int n,
int i )
58 {
59 int largest = i;
60 int l = 2 * i + 1;
61 int r = 2 * i + 2;
62
63 if (l < n && arr[l] > arr[largest])
64 largest = l;
65
66 if (r < n && arr[r] > arr[largest])
67 largest = r;
68
69 if (largest != i) {
70 std::swap(arr[i], arr[largest]);
71 heapify(arr, n, largest);
72 }
73}
T swap(T... args)

◆ heapSort()

template<typename T >
void heapSort ( T * arr,
int n )

Utilizes heapify procedure to sort the array

Parameters
arrarray to be sorted
nsize of array
84 {
85 for (int i = n - 1; i >= 0; i--) heapify(arr, n, i);
86
87 for (int i = n - 1; i >= 0; i--) {
88 std::swap(arr[0], arr[i]);
89 heapify(arr, i, 0);
90 }
91}
Here is the call graph for this function:

◆ main()

int main ( void )

Main function

Driver Code

90 {
91 int size;
92 std::cout << "Enter the number of elements : ";
93 std::cin >> size;
94 int *arr = new int[size];
95 std::cout << "Enter the unsorted elements : ";
96 for (int i = 0; i < size; ++i) {
97 std::cin >> arr[i];
98 }
99 mergeSort(arr, 0, size - 1);
100 std::cout << "Sorted array : ";
101 show(arr, size);
102 delete[] arr;
103 return 0;
104}
void mergeSort(int *arr, int l, int r)
Definition merge_sort.cpp:71
Here is the call graph for this function:

◆ merge()

void merge ( int * arr,
int l,
int m,
int r )

The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

Parameters
arr- array with two halves arr[l...m] and arr[m+1...r]
l- left index or start index of first half array
m- right index or end index of first half array

(The second array starts form m+1 and goes till r)

Parameters
r- end index or right index of second half array
33 {
34 int i, j, k;
35 int n1 = m - l + 1;
36 int n2 = r - m;
37
38 int *L = new int[n1], *R = new int[n2];
39
40 for (i = 0; i < n1; i++) L[i] = arr[l + i];
41 for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
42
43 i = 0;
44 j = 0;
45 k = l;
46 while (i < n1 || j < n2) {
47 if (j >= n2 || (i < n1 && L[i] <= R[j])) {
48 arr[k] = L[i];
49 i++;
50 } else {
51 arr[k] = R[j];
52 j++;
53 }
54 k++;
55 }
56
57 delete[] L;
58 delete[] R;
59}
double k(double x)
Another test function.
Definition composite_simpson_rule.cpp:117

◆ mergeSort()

void mergeSort ( int * arr,
int l,
int r )

Merge sort is a divide and conquer algorithm, it divides the input array into two halves and calls itself for the two halves and then calls merge() to merge the two halves

Parameters
arr- array to be sorted
l- left index or start index of array
r- right index or end index of array
71 {
72 if (l < r) {
73 int m = l + (r - l) / 2;
74 mergeSort(arr, l, m);
75 mergeSort(arr, m + 1, r);
76 merge(arr, l, m, r);
77 }
78}
void merge(int *arr, int l, int m, int r)
Definition merge_sort.cpp:33
Here is the call graph for this function:

◆ show()

void show ( int * arr,
int size )

Utility function used to print the array after sorting

84 {
85 for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
86 std::cout << "\n";
87}

◆ test()

static void test ( )
static

Test function

Returns
void
106 {
107 std::srand(std::time(nullptr)); // initialize random number generator
108
109 std::vector<float> data1(100);
110 for (auto &d : data1) { // generate random numbers between -5.0 and 4.99
111 d = float(std::rand() % 1000 - 500) / 100.f;
112 }
113
114 std::vector<float> sorted = sorting::wiggle_sort::wiggleSort<float>(data1);
115
116 displayElements(sorted);
117
118 for (uint32_t j = 0; j < data1.size(); j += 2) {
119 assert(data1[j] <= data1[j + 1] &&
120 data1[j + 1] >= data1[j + 2]); // check the validation condition
121 }
122
123 std::cout << "Test 1 passed\n";
124}
static void displayElements(const std::vector< T > &arr)
Utility function used for printing the elements. Prints elements of the array after they're sorted us...
Definition wiggle_sort.cpp:85
T rand(T... args)
T srand(T... args)
T time(T... args)
Here is the call graph for this function: