TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
recursive_bubble_sort.cpp File Reference

This is an implementation of a recursive version of the Bubble sort algorithm More...

#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for recursive_bubble_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 

Functions

template<typename T >
void sorting::recursive_bubble_sort (std::vector< T > *nums, uint64_t n)
 This is an implementation of the recursive_bubble_sort. A vector is passed to the function which is then dereferenced, so that the changes are reflected in the original vector. It also accepts a second parameter of type int and name n, which is the size of the array.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

This is an implementation of a recursive version of the Bubble sort algorithm

Author
Aditya Prakash

The working principle of the Bubble sort algorithm.

Bubble sort is a simple sorting algorithm used to rearrange a set of ascending or descending order elements. Bubble sort gets its name from the fact that data "bubbles" to the top of the dataset.

Algorithm

What is Swap?

Swapping two numbers means that we interchange their values. Often, an additional variable is required for this operation. This is further illustrated in the following:

void swap(int x, int y){ int z = x; x = y; y = z; }

The above process is a typical displacement process. When we assign a value to x, the old value of x is lost. That's why we create a temporary variable z to store the initial value of x. z is further used to assign the initial value of x to y, to complete swapping.

Recursion

While the recursive method does not necessarily have advantages over iterative versions, but it is useful to enhance the understanding of the algorithm and recursion itself. In Recursive Bubble sort algorithm, we firstly call the function on the entire array, and for every subsequent function call, we exclude the last element. This fixes the last element for that sub-array.Formally, for ith iteration, we consider elements up to n-i, where n is the number of elements in the array. Exit condition: n==1; i.e. the sub-array contains only one element.

Complexity Time complexity: O(n) best case; O(n²) average case; O(n²) worst case Space complexity: O(n)

We need to traverse the array n * (n-1) times. However, if the entire array is already sorted, then we need to traverse it only once. Hence, O(n) is the best case complexity

Definition in file recursive_bubble_sort.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 155 of file recursive_bubble_sort.cpp.

155 {
156 test(); // run self-test implementations
157 return 0;
158}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 105 of file recursive_bubble_sort.cpp.

105 {
106 // 1st example. Creating an array of type `int`.
107 std::cout << "1st test using `int`\n";
108 const uint64_t size = 6;
109 std::vector<int64_t> arr;
110 // populating the array
111 arr.push_back(22);
112 arr.push_back(46);
113 arr.push_back(94);
114 arr.push_back(12);
115 arr.push_back(37);
116 arr.push_back(63);
117 // array populating ends
118
120 assert(std::is_sorted(std::begin(arr), std::end(arr)));
121 std::cout << " 1st test passed!\n";
122 // printing the array
123 for (uint64_t i = 0; i < size; i++) {
124 std::cout << arr[i] << ", ";
125 }
126 std::cout << std::endl;
127
128 // 2nd example. Creating an array of type `double`.
129 std::cout << "2nd test using doubles\n";
130 std::vector<double> double_arr;
131
132 // populating the array
133 double_arr.push_back(20.4);
134 double_arr.push_back(62.7);
135 double_arr.push_back(12.2);
136 double_arr.push_back(43.6);
137 double_arr.push_back(74.1);
138 double_arr.push_back(57.9);
139 // array populating ends
140
141 sorting::recursive_bubble_sort(&double_arr, size);
142 assert(std::is_sorted(std::begin(double_arr), std::end(double_arr)));
143 std::cout << " 2nd test passed!\n";
144 // printing the array
145 for (uint64_t i = 0; i < size; i++) {
146 std::cout << double_arr[i] << ", ";
147 }
148 std::cout << std::endl;
149}
void recursive_bubble_sort(std::vector< T > *nums, uint64_t n)
This is an implementation of the recursive_bubble_sort. A vector is passed to the function which is t...