-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
139 lines (115 loc) · 4.04 KB
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Daniel Gelfand
//APCS1 pd2
//HW50 -- Dat Bubbly Tho
//2017-12-08
/******************************
* class BubbleSort -- implements bubblesort algorithm (vanilla)
******************************/
import java.util.ArrayList;
public class BubbleSort {
//~~~~~~~~~~~~~~~~~~~ HELPER METHODS ~~~~~~~~~~~~~~~~~~~
//precond: lo < hi && size > 0
//postcond: returns an ArrayList of random integers
// from lo to hi, inclusive
public static ArrayList populate( int size, int lo, int hi ) {
ArrayList<Integer> retAL = new ArrayList<Integer>();
while( size > 0 ) {
// offset + rand int on interval [lo,hi]
retAL.add( lo + (int)( (hi-lo+1) * Math.random() ) );
size--;
}
return retAL;
}
//randomly rearrange elements of an ArrayList
public static void shuffle( ArrayList al ) {
int randomIndex;
//setup for traversal fr right to left
for( int i = al.size()-1; i > 0; i-- ) {
//pick an index at random
randomIndex = (int)( (i+1) * Math.random() );
//swap the values at position i and randomIndex
al.set( i, al.set( randomIndex, al.get(i) ) );
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VOID version of bubbleSort
// Rearranges elements of input ArrayList
// postcondition: data's elements sorted in ascending order
public static void bubbleSortV( ArrayList<Comparable> data )
{
boolean swapped = true;
/* for(int i = 0; i < data.size()-1; i+=1){
//if sorting was no longer needed then we are done
if(!swapped) break;*/
while(swapped){
swapped = false; //possible swap later on
for(int j = 0; j < data.size()-1; j+=1){
//if element before is greater than element after -> swap
if(data.get(j).compareTo(data.get(j+1))> 0){
swap(data,j,j+1);
swapped = true; //swap occured
}
}
}
}
//HELPER FUNCTION FOR BUBBLESORT
//swaps adjacent elements given the ArrayList and two indexes.
public static void swap(ArrayList<Comparable> data,int a, int b){
Comparable temp = data.get(b);
data.set(b,data.get(a));
data.set(a,temp);
}
// ArrayList-returning bubbleSort
// postcondition: order of input ArrayList's elements unchanged
// Returns sorted copy of input ArrayList.
public static ArrayList<Comparable> bubbleSort( ArrayList<Comparable> input )
{
ArrayList<Comparable> temp = new ArrayList<Comparable>();
//put all elements of input into temp
for(int i = 0; i < input.size(); i +=1){
//System.out.println(temp);
temp.add(i,input.get(i));
}
//sort temp
bubbleSortV(temp);
return temp;
}
public static void main( String [] args )
{
/*===============for VOID methods=============
ArrayList glen = new ArrayList<Integer>();
glen.add(7);
glen.add(1);
glen.add(5);
glen.add(12);
glen.add(3);
System.out.println( "ArrayList glen before sorting:\n" + glen );
bubbleSortV(glen);
System.out.println( "ArrayList glen after sorting:\n" + glen );
ArrayList coco = populate( 10, 1, 1000 );
System.out.println( "ArrayList coco before sorting:\n" + coco );
bubbleSortV(coco);
System.out.println( "ArrayList coco after sorting:\n" + coco );
============================================*/
ArrayList glen = new ArrayList<Integer>();
glen.add(7);
glen.add(1);
glen.add(5);
glen.add(12);
glen.add(3);
System.out.println( "ArrayList glen before sorting:\n" + glen );
ArrayList glenSorted = bubbleSort( glen );
System.out.println( "sorted version of ArrayList glen:\n"
+ glenSorted );
System.out.println( "ArrayList glen after sorting:\n" + glen );
ArrayList coco = populate( 10, 1, 1000 );
System.out.println( "ArrayList coco before sorting:\n" + coco );
ArrayList cocoSorted = bubbleSort( coco );
System.out.println( "sorted version of ArrayList coco:\n"
+ cocoSorted );
System.out.println( "ArrayList coco after sorting:\n" + coco );
System.out.println( coco );
/*==========for AL-returning methods==========
============================================*/
}//end main
}//end class BubbleSort