TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
Sorting Algorithm

Files

file  merge_sort.cpp
 Merge Sort Algorithm (MERGE 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>
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.

Definition at line 86 of file wiggle_sort.cpp.

86 {
87 uint32_t size = arr.size();
88
89 std::cout << "Sorted elements are as follows: ";
90
91 std::cout << "[";
92
93 for (int i = 0; i < size; i++) {
94 std::cout << arr[i];
95 if (i != size - 1) {
96 std::cout << ", ";
97 }
98 }
99
100 std::cout << "]" << std::endl;
101}

◆ heapify()

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

Definition at line 58 of file heap_sort.cpp.

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}
double l(double x)
Another test function.

◆ 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

Definition at line 84 of file heap_sort.cpp.

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}

◆ main()

int main ( void )

Main function

Driver Code

Definition at line 101 of file merge_sort.cpp.

101 {
102 int size;
103 std::cout << "Enter the number of elements: ";
104 std::cin >> size;
105
106 if (size <= 0) {
107 std::cout << "Invalid size.\n";
108 return 1;
109 }
110
111 int *arr = new int[size];
112 std::cout << "Enter the unsorted elements: ";
113 for (int i = 0; i < size; ++i) {
114 std::cin >> arr[i];
115 }
116
117 mergeSort(arr, 0, size - 1);
118 std::cout << "Sorted array: ";
119 show(arr, size);
120 delete[] arr;
121 return 0;
122}
void show(int *arr, int size)
void mergeSort(int *arr, int l, int r)

◆ 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

Definition at line 37 of file merge_sort.cpp.

37 {
38 int n1 = m - l + 1;
39 int n2 = r - m;
40
41 std::vector<int> L(n1), R(n2);
42
43 for (int i = 0; i < n1; i++) L[i] = arr[l + i];
44 for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
45
46 int i = 0, j = 0, k = l;
47
48 while (i < n1 && j < n2) {
49 if (L[i] <= R[j]) {
50 arr[k] = L[i];
51 i++;
52 } else {
53 arr[k] = R[j];
54 j++;
55 }
56 k++;
57 }
58
59 while (i < n1) {
60 arr[k] = L[i];
61 i++;
62 k++;
63 }
64
65 while (j < n2) {
66 arr[k] = R[j];
67 j++;
68 k++;
69 }
70}
double k(double x)
Another test function.

◆ 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

Definition at line 82 of file merge_sort.cpp.

82 {
83 if (l < r) {
84 int m = l + (r - l) / 2;
85 mergeSort(arr, l, m);
86 mergeSort(arr, m + 1, r);
87 merge(arr, l, m, r);
88 }
89}
void merge(int *arr, int l, int m, int r)

◆ show()

void show ( int * arr,
int size )

Utility function used to print the array after sorting

Definition at line 95 of file merge_sort.cpp.

95 {
96 for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
97 std::cout << "\n";
98}

◆ test()

void test ( )
static

Test function

Returns
void

Definition at line 107 of file wiggle_sort.cpp.

107 {
108 std::srand(std::time(nullptr)); // initialize random number generator
109
110 std::vector<float> data1(100);
111 for (auto &d : data1) { // generate random numbers between -5.0 and 4.99
112 d = float(std::rand() % 1000 - 500) / 100.f;
113 }
114
115 std::vector<float> sorted = sorting::wiggle_sort::wiggleSort<float>(data1);
116
117 displayElements(sorted);
118
119 for (uint32_t j = 0; j < data1.size(); j += 2) {
120 assert(data1[j] <= data1[j + 1] &&
121 data1[j + 1] >= data1[j + 2]); // check the validation condition
122 }
123
124 std::cout << "Test 1 passed\n";
125}
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...
std::vector< T > wiggleSort(const std::vector< T > &arr)
Function used for sorting the elements in wave form.