首页 > 代码库 > Array; Bubble sort

Array; Bubble sort

 1 package com.java7; 2  3 class Bubble { 4     public static void main(String[] args) { 5         int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 }; 6         int a, b, t; 7         int size; 8          9         size = 10; // number of elements to sort10         11         // display original array12         System.out.print("Original array is: ");13         for(int i = 0; i < size; i++)14             System.out.print(" " + nums[i]);15         System.out.println();16         17         for(a = 1; a < size; a++)18             for(b = size - 1; b >= a; b--) {19                 if(nums[b-1] > nums[b]) { // if out of order20                     // exchange elements21                     t = nums[b-1];22                     nums[b-1] = nums[b];23                     nums[b] = t;24                 }25             }26         27         // display sorted array28         System.out.print("Sorted array is: ");29         for(int i = 0; i < size; i++)30             System.out.print(" " + nums[i]);31         System.out.println();32         33     }34 }

 

Array; Bubble sort