/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package introsortb; import java.util.Random; /** * * @author jcmtiernan */ public class IntroSortb { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAX = 10; int[] array2Sort = new int[MAX]; Random rand = new Random(); int randomNumber; for(int i = 0; i < MAX; i++) { randomNumber = rand.nextInt(1000); // 0-999. array2Sort[i] = randomNumber; } for(int i = 0; i < MAX; i++) { System.out.println( array2Sort[i]); } /* want to sort 5, 9, 2, 8 [0] 5 5 2 2 [1] 9 2 5 5 [2] 2 8 8 8 [3] 8 9 9 9 swaps 2 1 0 */ int swap; int swapCount = 1000; int numSwaps = MAX; // Bubble Sort algorithm for(int j=0; (j array2Sort[i+1] ) { //swap swap = array2Sort[i+1]; array2Sort[i+1] = array2Sort[i]; array2Sort[i] = swap; swapCount++; } /* System.out.println("Comparing arr["+i+"] of array2Sort[i] "+array2Sort[i]+ " with arr["+(i+1)+"] of array2Sort[i+1] "+array2Sort[(i+1)]); */ } System.out.println(); System.out.println(); for(int i = 0; i < MAX; i++) { System.out.println( array2Sort[i]); } System.out.println("SwapCount is "+swapCount); } } }