Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // package whatever; // don't place package name!
- import java.io.*;
- /**
- * Quadratic Sorts
- *
- *
- * Problem 3: Bubble Sort
- *
- * Prompt: Given an unsorted array of integers, return the array
- * sorted using bubble sort.
- *
- * Input: input {Array}
- * Output: {Array}
- *
- * Example: [3,9,1,4,7] --> [1,3,4,7,9]
- */
- import java.util.*;
- class BasicSort {
- public static void main (String[] args) {
- int[] arr = {0};
- System.out.println("output" + Arrays.toString(bubble(arr)));
- }
- public static int[] bubble(int[] input) {
- int index = 0;
- int j = 0;
- int temp = 0;
- int len = input.length;
- boolean swappedFlag;
- for(index = 0; index < len -1; index++){
- swappedFlag = false;
- System.out.println("index: " + index);
- // j resets to first element after every pass
- for(j = 0; j < len -1 -index; j++){
- System.out.println("j : " + j);
- if(input[j] > input[j+1]){
- temp = input[j];
- input[j] = input[j+1];
- input[j+1] = temp;
- swappedFlag = true;
- }
- }
- //if now swap made, break from the loop
- if(swappedFlag == false){
- //no swaps made in the last pass
- //therefore elements are sorted, break
- break;
- }
- }
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement