Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BubbleSort recursive implementation in Java #274

Merged
merged 3 commits into from
Oct 16, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BubbleSortRecursive.java: Add BubbleSort Algo
This adds the recursive implementation of BubbleSort Algorithm in Java.
The time complexity of the algorithm is O(n^2).

Closes #18
sameerchoubey authored and gitmate-bot committed Oct 16, 2018
commit a5398acf1544ff6e3336db07ab796d565a417d3e
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ This repository contains examples of various algorithms written on different pro
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) |[:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | [:octocat:](bubble_sort/Java) | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |
| [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python)|
40 changes: 40 additions & 0 deletions bubble_sort/Java/BubbleSortRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class BubbleSortRecursive {
public static void main(String[] args){
//Taking a sample array for sorting it.
int[] nums = {12, 45, 23, 78, 67};

//calling the method and passing the array in it.
bubble(nums, nums.length);
//Printing the sorted array
printSortedArray(nums);
}
//The recurisve method that takes in the array and the length of it.
public static void bubble(int[] nums, int end){
//base case
if (end == 1) {
return;
}
//for loop that iterates and sorts the sorts the array
for (int i = 0; i < end - 1; i++) {
if (nums[i] > nums[i + 1]) {
int temp = nums[i];
nums[i] = nums[i + 1];
sangamcse marked this conversation as resolved.
Show resolved Hide resolved
nums[i + 1] = temp;
}
}
//calling the method again to sort the rest of the elements on next iteration
sangamcse marked this conversation as resolved.
Show resolved Hide resolved
bubble(nums, end - 1);
}

sangamcse marked this conversation as resolved.
Show resolved Hide resolved
//Method to print an array
public static void printSortedArray(int arr[]) {

//storing the length of an array using the .length function in Java
int n = arr.length;
sangamcse marked this conversation as resolved.
Show resolved Hide resolved
//For loop that iterates over each element of the array and then prints it.
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

}