Spring Semester 2005

Practical Exam Notes: Bubble Sort and Selection Sort
Here's the code developed in class on Tuesday (April 12):
```class Bubble {
public static void main(String[] args) {
int[] a = new int[args.length];
for (int i = 0; i < args.length; i++)
a[i] = Integer.parseInt(args[i]);
Utilities.show(a);
boolean sorted;
do {
sorted = true;
for (int i = 0; i < a.length - 1; i++)
if (a[i] > a[i+1]) {
// Utilities.show(a);
sorted = false;
int temp = a[i+1];
a[i+1] = a[i];
a[i] = temp;
Utilities.show(a);
}
System.out.println("-----------------------");
} while (! sorted);
}
}```
This relies on a utilities method:
```class Utilities {
public static void show(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
} ```
And here's the second method of sorting:
```class Selection
{ public static void main(String[] args)
{ int[] a = new int[args.length];
for (int i = 0; i < args.length; i++)
a[i] = Integer.parseInt(args[i]);
System.out.print("       ");
Utilities.show(a);
int i = 0, j = 0;
for (i = 0; i < a.length; i++)
{ for (j = i + 1; j < a.length; j++)
{ if (a[i] > a[j])
{ int temp = a[i];
a[i] = a[j];
a[j] = temp;
System.out.print("(" + i + ", " + j + ") ");
Utilities.show(a);
}
System.out.println("-----------------------------(" + i + ", " + j + ") ");
}
System.out.print("       ");
Utilities.show(a);
System.out.println("-----------------------------(" + i + ", " + j + ") ");
}
}
}```
Here's `Bubble.java`, run twice:
```frilled.cs.indiana.edu%java Bubble 2 1 4 3 6 7 5
2 1 4 3 6 7 5
1 2 4 3 6 7 5
1 2 3 4 6 7 5
1 2 3 4 6 5 7
-----------------------
1 2 3 4 5 6 7
-----------------------
-----------------------
frilled.cs.indiana.edu%java Bubble 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 8 6 5 4 3 2 1
7 6 8 5 4 3 2 1
7 6 5 8 4 3 2 1
7 6 5 4 8 3 2 1
7 6 5 4 3 8 2 1
7 6 5 4 3 2 8 1
7 6 5 4 3 2 1 8
-----------------------
6 7 5 4 3 2 1 8
6 5 7 4 3 2 1 8
6 5 4 7 3 2 1 8
6 5 4 3 7 2 1 8
6 5 4 3 2 7 1 8
6 5 4 3 2 1 7 8
-----------------------
5 6 4 3 2 1 7 8
5 4 6 3 2 1 7 8
5 4 3 6 2 1 7 8
5 4 3 2 6 1 7 8
5 4 3 2 1 6 7 8
-----------------------
4 5 3 2 1 6 7 8
4 3 5 2 1 6 7 8
4 3 2 5 1 6 7 8
4 3 2 1 5 6 7 8
-----------------------
3 4 2 1 5 6 7 8
3 2 4 1 5 6 7 8
3 2 1 4 5 6 7 8
-----------------------
2 3 1 4 5 6 7 8
2 1 3 4 5 6 7 8
-----------------------
1 2 3 4 5 6 7 8
-----------------------
-----------------------
frilled.cs.indiana.edu%```
Here's `Selection` being run with the same sequences as input:
```sluggo%java Selection 2 1 4 3 6 5 8 7
2 1 4 3 6 5 8 7
(0, 1) 1 2 4 3 6 5 8 7
-----------------------------(0, 1)
-----------------------------(0, 2)
-----------------------------(0, 3)
-----------------------------(0, 4)
-----------------------------(0, 5)
-----------------------------(0, 6)
-----------------------------(0, 7)
1 2 4 3 6 5 8 7
-----------------------------(0, 8)
-----------------------------(1, 2)
-----------------------------(1, 3)
-----------------------------(1, 4)
-----------------------------(1, 5)
-----------------------------(1, 6)
-----------------------------(1, 7)
1 2 4 3 6 5 8 7
-----------------------------(1, 8)
(2, 3) 1 2 3 4 6 5 8 7
-----------------------------(2, 3)
-----------------------------(2, 4)
-----------------------------(2, 5)
-----------------------------(2, 6)
-----------------------------(2, 7)
1 2 3 4 6 5 8 7
-----------------------------(2, 8)
-----------------------------(3, 4)
-----------------------------(3, 5)
-----------------------------(3, 6)
-----------------------------(3, 7)
1 2 3 4 6 5 8 7
-----------------------------(3, 8)
(4, 5) 1 2 3 4 5 6 8 7
-----------------------------(4, 5)
-----------------------------(4, 6)
-----------------------------(4, 7)
1 2 3 4 5 6 8 7
-----------------------------(4, 8)
-----------------------------(5, 6)
-----------------------------(5, 7)
1 2 3 4 5 6 8 7
-----------------------------(5, 8)
(6, 7) 1 2 3 4 5 6 7 8
-----------------------------(6, 7)
1 2 3 4 5 6 7 8
-----------------------------(6, 8)
1 2 3 4 5 6 7 8
-----------------------------(7, 8)
sluggo%java Selection 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
(0, 1) 7 8 6 5 4 3 2 1
-----------------------------(0, 1)
(0, 2) 6 8 7 5 4 3 2 1
-----------------------------(0, 2)
(0, 3) 5 8 7 6 4 3 2 1
-----------------------------(0, 3)
(0, 4) 4 8 7 6 5 3 2 1
-----------------------------(0, 4)
(0, 5) 3 8 7 6 5 4 2 1
-----------------------------(0, 5)
(0, 6) 2 8 7 6 5 4 3 1
-----------------------------(0, 6)
(0, 7) 1 8 7 6 5 4 3 2
-----------------------------(0, 7)
1 8 7 6 5 4 3 2
-----------------------------(0, 8)
(1, 2) 1 7 8 6 5 4 3 2
-----------------------------(1, 2)
(1, 3) 1 6 8 7 5 4 3 2
-----------------------------(1, 3)
(1, 4) 1 5 8 7 6 4 3 2
-----------------------------(1, 4)
(1, 5) 1 4 8 7 6 5 3 2
-----------------------------(1, 5)
(1, 6) 1 3 8 7 6 5 4 2
-----------------------------(1, 6)
(1, 7) 1 2 8 7 6 5 4 3
-----------------------------(1, 7)
1 2 8 7 6 5 4 3
-----------------------------(1, 8)
(2, 3) 1 2 7 8 6 5 4 3
-----------------------------(2, 3)
(2, 4) 1 2 6 8 7 5 4 3
-----------------------------(2, 4)
(2, 5) 1 2 5 8 7 6 4 3
-----------------------------(2, 5)
(2, 6) 1 2 4 8 7 6 5 3
-----------------------------(2, 6)
(2, 7) 1 2 3 8 7 6 5 4
-----------------------------(2, 7)
1 2 3 8 7 6 5 4
-----------------------------(2, 8)
(3, 4) 1 2 3 7 8 6 5 4
-----------------------------(3, 4)
(3, 5) 1 2 3 6 8 7 5 4
-----------------------------(3, 5)
(3, 6) 1 2 3 5 8 7 6 4
-----------------------------(3, 6)
(3, 7) 1 2 3 4 8 7 6 5
-----------------------------(3, 7)
1 2 3 4 8 7 6 5
-----------------------------(3, 8)
(4, 5) 1 2 3 4 7 8 6 5
-----------------------------(4, 5)
(4, 6) 1 2 3 4 6 8 7 5
-----------------------------(4, 6)
(4, 7) 1 2 3 4 5 8 7 6
-----------------------------(4, 7)
1 2 3 4 5 8 7 6
-----------------------------(4, 8)
(5, 6) 1 2 3 4 5 7 8 6
-----------------------------(5, 6)
(5, 7) 1 2 3 4 5 6 8 7
-----------------------------(5, 7)
1 2 3 4 5 6 8 7
-----------------------------(5, 8)
(6, 7) 1 2 3 4 5 6 7 8
-----------------------------(6, 7)
1 2 3 4 5 6 7 8
-----------------------------(6, 8)
1 2 3 4 5 6 7 8
-----------------------------(7, 8)```
One is more verbose than the other, because we wrote them so.

Last updated by Adrian German for A201/A597/I210 on `Wed Apr 13 12:28:37 EST 2005`