-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bubble_sort.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
- Loading branch information
1 parent
222206a
commit b861fff
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
public class bubble_sort { | ||
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 | ||
print_sorted_array(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]; | ||
nums[i + 1] = temp; | ||
} | ||
} | ||
//calling the method again to sort the rest of the elements on next iteration | ||
bubble(nums, end - 1); | ||
} | ||
|
||
//Method to print an array | ||
public static void print_sorted_array(int arr[]) { | ||
|
||
//storing the length of an array using the .length function in Java | ||
int n = arr.length; | ||
//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(); | ||
} | ||
|
||
} |