| 3 | //Problem : Bubble Sort |
| 4 | |
| 5 | public class BubbleSort { |
| 6 | public static void bubbleSort(int arr[]) { |
| 7 | for(int turn=0; turn<arr.length-1; turn++) { |
| 8 | for(int j=0; j<arr.length-1-turn; j++) { |
| 9 | if(arr[j] > arr[j+1]) { |
| 10 | //swap |
| 11 | int temp = arr[j]; |
| 12 | arr[j] = arr[j+1]; |
| 13 | arr[j+1] = temp; |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | public static void modifiedBubbleSort(int arr[]) { |
| 20 | for(int turn=0; turn<arr.length-1; turn++) { |
| 21 | boolean swapped = false; |
| 22 | for(int j=0; j<arr.length-1-turn; j++) { |
| 23 | if(arr[j] > arr[j+1]) { |
| 24 | //swap |
| 25 | int temp = arr[j]; |
| 26 | arr[j] = arr[j+1]; |
| 27 | arr[j+1] = temp; |
| 28 | swapped = true; |
| 29 | } |
| 30 | } |
| 31 | if(swapped == false) { |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public static void bubbleSortDescending(int arr[]) { |
| 38 | for(int turn=0; turn<arr.length-1; turn++) { |
| 39 | for(int j=0; j<arr.length-1-turn; j++) { |
| 40 | if(arr[j] < arr[j+1]) { |
| 41 | //swap |
| 42 | int temp = arr[j]; |
| 43 | arr[j] = arr[j+1]; |
| 44 | arr[j+1] = temp; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static void printArr(int arr[]) { |
| 51 | for(int i=0; i<arr.length; i++) { |
| 52 | System.out.print(arr[i]+" "); |
| 53 | } |
| 54 | System.out.println(); |
| 55 | } |
| 56 | public static void main(String args[]) { |
| 57 | int arr[] = {5, 4, 3, 2, 1}; |
| 58 | bubbleSortDescending(arr); |
| 59 | printArr(arr); |
| 60 | } |
| 61 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…