Add example for bubble sort
This commit is contained in:
		
							parent
							
								
									8f70278ac6
								
							
						
					
					
						commit
						2f94a59567
					
				| @ -18,3 +18,4 @@ project ( | ||||
| add_subdirectory(merge) | ||||
| add_subdirectory(selection) | ||||
| add_subdirectory(insertion) | ||||
| add_subdirectory(bubble) | ||||
|  | ||||
							
								
								
									
										17
									
								
								cpp/algorithms/sorting/bubble/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								cpp/algorithms/sorting/bubble/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | ||||
| ############################################################################### | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2021 Shaun Reed, all rights reserved               ## | ||||
| ## About: A basic CMakeLists configuration to practice bubble sort           ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################### | ||||
| 
 | ||||
| cmake_minimum_required(VERSION 3.16) | ||||
| project(BubbleSort LANGUAGES CXX) | ||||
| 
 | ||||
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | ||||
| 
 | ||||
| add_executable(bubble-sort "bubble-sort.cpp") | ||||
| 
 | ||||
| add_library(lib-bubble "lib-bubble.cpp") | ||||
| target_link_libraries(bubble-sort lib-bubble) | ||||
							
								
								
									
										37
									
								
								cpp/algorithms/sorting/bubble/bubble-sort.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								cpp/algorithms/sorting/bubble/bubble-sort.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2021 Shaun Reed, all rights reserved               ## | ||||
| ## About: An example implementation of bubble sort using a custom library    ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################### | ||||
| */ | ||||
| 
 | ||||
| #include "lib-bubble.hpp" | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <vector> | ||||
| #include <random> | ||||
| #include <iostream> | ||||
| 
 | ||||
| int main(const int argc, const char * argv[]) | ||||
| { | ||||
|   srand(time(nullptr)); | ||||
|   std::vector<int> array(ARRAY_LENGTH); | ||||
|   std::generate(array.begin(), array.end(), [](){ return rand() % 1000;}); | ||||
| 
 | ||||
|   auto print = [](std::vector<int> array) { | ||||
|     for (const auto &i : array) { | ||||
|       std::cout << i << ", "; | ||||
|     } | ||||
|     std::cout << std::endl; | ||||
|   }; | ||||
| 
 | ||||
|   std::cout << "Unsorted array: \n"; | ||||
|   print(array); | ||||
| 
 | ||||
|   BubbleSort(array); | ||||
| 
 | ||||
|   std::cout << "Sorted array: \n"; | ||||
|   print(array); | ||||
| } | ||||
							
								
								
									
										29
									
								
								cpp/algorithms/sorting/bubble/lib-bubble.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								cpp/algorithms/sorting/bubble/lib-bubble.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2021 Shaun Reed, all rights reserved               ## | ||||
| ## About: An example implementation of bubble sort using a custom library    ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################### | ||||
| */ | ||||
| 
 | ||||
| #include "lib-bubble.hpp" | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <vector> | ||||
| 
 | ||||
| void BubbleSort(std::vector<int> &array) | ||||
| { | ||||
|   // For each value within the set, starting at 0
 | ||||
|   for (int sortedPivot = 0; sortedPivot < array.size(); sortedPivot++) { | ||||
|     // Check every other remaining value in the set
 | ||||
|     for (int j = array.size() - 1; j > sortedPivot; j--) { | ||||
|       // Swap if the value at j is less than the value before it
 | ||||
|       if (array[j] < array[j - 1]) { | ||||
|         std::swap(array[j], array[j - 1]); | ||||
|       } | ||||
|     } | ||||
|     // Increment sortedPivot, marking the lhs portion of the set as sorted
 | ||||
|   } | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										19
									
								
								cpp/algorithms/sorting/bubble/lib-bubble.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								cpp/algorithms/sorting/bubble/lib-bubble.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2021 Shaun Reed, all rights reserved               ## | ||||
| ## About: An example implementation of bubble sort using a custom library    ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################### | ||||
| */ | ||||
| 
 | ||||
| #ifndef LIB_BUBBLE_HPP | ||||
| #define LIB_BUBBLE_HPP | ||||
| 
 | ||||
| #include <vector> | ||||
| 
 | ||||
| #define ARRAY_LENGTH 10 | ||||
| 
 | ||||
| void BubbleSort(std::vector<int> &array); | ||||
| 
 | ||||
| #endif // LIB_BUBBLE_HPP
 | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user