BubbleSort Implementation

2019-01-26 02:30发布

I tried to make an implementation of bubble sort, but I am not sure whether it is correct or not. If you can give it a look and if it is a bubble sort and can be done in better way please don't be shy. Here is the code:

package Exercises;

import java.util.*;

public class BubbleSort_6_18 
{
    public static void main(String[] args) 
    {
        Random generator = new Random();

        int[] list = new int[11];
        for(int i=0; i<list.length; i++)
        {
            list[i] = generator.nextInt(10);
        }

        System.out.println("Original Random array: ");
        printArray(list);

        bubbleSort(list);

        System.out.println("\nAfter bubble sort: ");
        printArray(list);
    }

    public static void bubbleSort(int[] list)
    {
        for(int i=0; i<list.length; i++)
        {
            for(int j=i + 1; j<list.length; j++)
            {
                if(list[i] > list[j])
                {
                    int temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }

        }
    }

    public static void printArray(int[] list)
    {
        for(int i=0; i<list.length; i++)
        {
            System.out.print(list[i] + ", ");
        }
    }
}

14条回答
混吃等死
2楼-- · 2019-01-26 02:46
    int[] nums = new int[] { 6, 3, 2, 1, 7, 10, 9 };

        for(int i = nums.Length-1; i>=0; i--)
            for(int j = 0; j<i; j++)
            {
                int temp = 0;
                if( nums[j] < nums[j + 1])
                {
                    temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-26 02:52
public class BubbleSort {
    public static void main(String[] args) {
        int arr[] = {64, 34, 25, 12, 22, 11, 90};
        BubbleSort client=new BubbleSort();
        int[] result=client.bubbleSort(arr);
        for(int i:result)
        {
            System.out.println(i);
        }
    }
    public int[] bubbleSort(int[] arr)
    {
        int n=arr.length;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-i-1;j++) 
                if(arr[j]>arr[j+1])
             swap(arr,j,j+1);   
        }
        return arr;
    }
    private int[] swap(int[] arr, int i, int j) {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
        return arr;
    }


}
查看更多
成全新的幸福
4楼-- · 2019-01-26 02:55
  • You can loop over the array until no more elements are swapped
  • When you put the element at the last position you know it's the largest, so you can recuce the inner loop by 1
查看更多
放荡不羁爱自由
5楼-- · 2019-01-26 02:58
/*
Implementation of Bubble sort using Java
*/

import java.util.Arrays;
import java.util.Scanner;


public class BubbleSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the number of elements of array");
            int n = in.nextInt();
            int []a = new int[n];
            System.out.println("Enter the integer array");
            for(int i=0; i<a.length; i++)
            {
                a[i]=in.nextInt();
            }
            System.out.println("UnSorted array: "+ Arrays.toString(a));         
            for(int i=0; i<n; i++)
            {
                for(int j=1; j<n; j++)
                {
                    if(a[j-1]>a[j])
                    {
                        int temp = a[j-1];
                        a[j-1]=a[j];
                        a[j]=temp;
                    }
                }
            }
            System.out.println("Sorted array: "+ Arrays.toString(a));           
    }
}


/*
****************************************
Time Complexity: O(n*n)
Space Complexity: O(1)
****************************************
*/
查看更多
Rolldiameter
6楼-- · 2019-01-26 03:00

I think you got the idea of bubble sort by looking at your code:

Bubble sort usually works like the following: Assume aNumber is some random number:

for (int i = 0; i < aNumber; i++)
{
     for(int j = 0; j < aNumber; j++)

      //Doing something with i and j, usually running it as a loop for 2D array
      //array[i][j] will give you a complete sort.
}

How bubble sort is it iterates through every single possible spot of the array. i x j times The down side to this is, it will take square the number of times to sort something. Not very efficient, but it does get the work done in the most easiest way.

查看更多
戒情不戒烟
7楼-- · 2019-01-26 03:00
class BubbleSort {

public static void main(String[] args) {

    int a[] = {5,4,3,2,1};
    int length = a.length - 1;

    for (int i = 0 ; i < length ; i++) {
        for (int j = 0 ; j < length-i ; j++) {
            if (a[j] > a[j+1]) {
                int swap = a[j];
                a[j] = a[j+1];
                a[j+1] = swap;
            }
        }
    }

    for (int x : a) {
        System.out.println(x);
    }
}

}

查看更多
登录 后发表回答