Java Tutorial/Collections/Sort
Содержание
- 1 Bubble Sort
- 2 Generic Merge Sorter with generic Comparator
- 3 Insertion Sort
- 4 Mergesort: merging two arrays into a third
- 5 Quicksort: simple version of quick sort
- 6 Quick sort: uses an insertion sort to handle subarrays of fewer than 10 cells
- 7 Quick sort with median-of-three partitioning
- 8 Selection Sort
- 9 Shellsort
- 10 Sorting Objects using insertion sort
Bubble Sort
Here are the rules:
- Compare two players.
- If the one on the left is taller, swap them.
- Move one position right.
public class MainClass {
public static void main(String[] args) {
int[] intArray = new int[] { 2, 6, 3, 8, 4, 9, 1 };
for (int i : intArray) {
System.out.print(i);
}
System.out.println();
bubbleSort(intArray);
for (int i : intArray) {
System.out.print(i);
}
}
public static void bubbleSort(int[] intArray) {
int out, in;
for (out = intArray.length - 1; out > 0; out--) {
for (in = 0; in < out; in++) {
if (intArray[in] > intArray[in + 1]) {
swap(intArray, in, in + 1);
}
}
}
}
private static void swap(int[] intArray, int one, int two) {
int temp = intArray[one];
intArray[one] = intArray[two];
intArray[two] = temp;
}
}
2638491 1234689
Generic Merge Sorter with generic Comparator
import java.util.ruparator;
public class AnimationTester {
public static void main(String[] args) {
Integer[] values = new Integer[]{1,2,7,3,5};
Comparator<Integer> comp = new Comparator<Integer>() {
public int compare(Integer d1, Integer d2) {
return d1.rupareTo(d2);
}
};
MergeSorter.sort(values, comp);
for (int i = 0; i < values.length; i++){
System.out.print(values[i]+" ");
}
}
}
class MergeSorter {
public static <E> void sort(E[] a, Comparator<? super E> comp) {
mergeSort(a, 0, a.length - 1, comp);
}
private static <E> void mergeSort(E[] a, int from, int to, Comparator<? super E> comp) {
if (from == to)
return;
int mid = (from + to) / 2;
// Sort the first and the second half
mergeSort(a, from, mid, comp);
mergeSort(a, mid + 1, to, comp);
merge(a, from, mid, to, comp);
}
private static <E> void merge(E[] a, int from, int mid, int to, Comparator<? super E> comp) {
int n = to - from + 1;
Object[] values = new Object[n];
int fromValue = from;
int middleValue = mid + 1;
int index = 0;
while (fromValue <= mid && middleValue <= to) {
if (comp.rupare(a[fromValue], a[middleValue]) < 0) {
values[index] = a[fromValue];
fromValue++;
} else {
values[index] = a[middleValue];
middleValue++;
}
index++;
}
while (fromValue <= mid) {
values[index] = a[fromValue];
fromValue++;
index++;
}
while (middleValue <= to) {
values[index] = a[middleValue];
middleValue++;
index++;
}
for (index = 0; index < n; index++)
a[from + index] = (E) values[index];
}
}
Insertion Sort
public class MainClass {
public static void main(String[] args) {
int[] intArray = new int[] { 2, 6, 3, 8, 4, 9, 1 };
for (int i : intArray) {
System.out.print(i);
}
System.out.println();
insertionSort(intArray);
for (int i : intArray) {
System.out.print(i);
}
}
public static void insertionSort(int[] intArray) {
int in, out;
for (out = 1; out < intArray.length; out++) {
int temp = intArray[out];
in = out;
while (in > 0 && intArray[in - 1] >= temp) {
intArray[in] = intArray[in - 1];
--in;
}
intArray[in] = temp;
}
}
private static void swap(int[] intArray, int one, int two) {
int temp = intArray[one];
intArray[one] = intArray[two];
intArray[two] = temp;
}
}
2638491 1234689
Mergesort: merging two arrays into a third
public class MainClass {
public static void main(String[] args) {
int[] arrayA = { 23, 47, 81, 95 };
int[] arrayB = { 7, 14, 39, 55, 62, 74 };
int[] arrayC = new int[10];
merge(arrayA, arrayA.length, arrayB, arrayB.length, arrayC);
for (int i : arrayC) {
System.out.println(i);
}
}
public static void merge(int[] arrayA, int sizeA, int[] arrayB, int sizeB, int[] arrayC) {
int arrayAIndex = 0, arrayBIndex = 0, arrayCIndex = 0;
while (arrayAIndex < sizeA && arrayBIndex < sizeB)
if (arrayA[arrayAIndex] < arrayB[arrayBIndex])
arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
else
arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
while (arrayAIndex < sizeA)
arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
while (arrayBIndex < sizeB)
arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
}
}
7 14 23 39 47 55 62 74 81 95
Quicksort: simple version of quick sort
public class MainClass {
public static void main(String[] args) {
int[] intArray = { 1, 9, 2, 8, 3, 7, 4, 6, 5 };
for (int i : intArray) {
System.out.println(i);
}
quickSort(intArray);
for (int i : intArray) {
System.out.println(i);
}
}
public static void quickSort(int[] intArray) {
recQuickSort(intArray, 0, intArray.length - 1);
}
private static void recQuickSort(int[] intArray, int left, int right) {
if (right - left <= 0)
return;
else {
int pivot = intArray[right];
int partition = partitionIt(intArray, left, right, pivot);
recQuickSort(intArray, left, partition - 1);
recQuickSort(intArray, partition + 1, right);
}
}
private static int partitionIt(int[] intArray, int left, int right, int pivot) {
int leftPtr = left - 1;
int rightPtr = right;
while (true) {
while (intArray[++leftPtr] < pivot)
;
while (rightPtr > 0 && intArray[--rightPtr] > pivot)
;
if (leftPtr >= rightPtr)
break;
else
swap(intArray, leftPtr, rightPtr);
}
swap(intArray, leftPtr, right);
return leftPtr;
}
private static void swap(int[] intArray, int dex1, int dex2) {
int temp = intArray[dex1];
intArray[dex1] = intArray[dex2];
intArray[dex2] = temp;
}
}
1 9 2 8 3 7 4 6 5 1 2 3 4 5 6 7 8 9
Quick sort: uses an insertion sort to handle subarrays of fewer than 10 cells
public class MainClass {
public static void main(String[] args) {
int[] intArray = { 1, 9, 2, 8, 3, 7, 4, 6, 5 };
for (int i : intArray) {
System.out.println(i);
}
quickSort(intArray);
for (int i : intArray) {
System.out.println(i);
}
}
public static void quickSort(int[] intArray) {
recQuickSort(intArray, 0, intArray.length - 1);
insertionSort(intArray, 0, intArray.length - 1);
}
public static void recQuickSort(int[] intArray, int left, int right) {
int size = right - left + 1;
if (size < 10)
insertionSort(intArray, left, right);
else {
double median = medianOf3(intArray, left, right);
int partition = partitionIt(intArray, left, right, median);
recQuickSort(intArray, left, partition - 1);
recQuickSort(intArray, partition + 1, right);
}
}
public static double medianOf3(int[] intArray, int left, int right) {
int center = (left + right) / 2;
if (intArray[left] > intArray[center])
swap(intArray, left, center);
if (intArray[left] > intArray[right])
swap(intArray, left, right);
if (intArray[center] > intArray[right])
swap(intArray, center, right);
swap(intArray, center, right - 1);
return intArray[right - 1];
}
public static void swap(int[] intArray, int dex1, int dex2) {
int temp = intArray[dex1];
intArray[dex1] = intArray[dex2];
intArray[dex2] = temp;
}
public static int partitionIt(int[] intArray, int left, int right, double pivot) {
int leftPtr = left;
int rightPtr = right - 1;
while (true) {
while (intArray[++leftPtr] < pivot)
;
while (intArray[--rightPtr] > pivot)
;
if (leftPtr >= rightPtr)
break;
else
swap(intArray, leftPtr, rightPtr);
}
swap(intArray, leftPtr, right - 1);
return leftPtr;
}
public static void insertionSort(int[] intArray, int left, int right) {
int in, out;
for (out = left + 1; out <= right; out++) {
int temp = intArray[out];
in = out;
while (in > left && intArray[in - 1] >= temp) {
intArray[in] = intArray[in - 1];
--in;
}
intArray[in] = temp;
}
}
}
1 9 2 8 3 7 4 6 5 1 2 3 4 5 6 7 8 9
Quick sort with median-of-three partitioning
public class MainClass {
public static void main(String[] args) {
int[] intArray = { 1, 9, 2, 8, 3, 7, 4, 6, 5 };
for (int i : intArray) {
System.out.println(i);
}
quickSort(intArray);
for (int i : intArray) {
System.out.println(i);
}
}
public static void quickSort(int[] intArray) {
recQuickSort(intArray, 0, intArray.length - 1);
}
public static void recQuickSort(int[] intArray, int left, int right) {
int size = right - left + 1;
if (size <= 3)
manualSort(intArray, left, right);
else {
double median = medianOf3(intArray, left, right);
int partition = partitionIt(intArray, left, right, median);
recQuickSort(intArray, left, partition - 1);
recQuickSort(intArray, partition + 1, right);
}
}
public static int medianOf3(int[] intArray, int left, int right) {
int center = (left + right) / 2;
if (intArray[left] > intArray[center])
swap(intArray, left, center);
if (intArray[left] > intArray[right])
swap(intArray, left, right);
if (intArray[center] > intArray[right])
swap(intArray, center, right);
swap(intArray, center, right - 1);
return intArray[right - 1];
}
public static void swap(int[] intArray, int dex1, int dex2) {
int temp = intArray[dex1];
intArray[dex1] = intArray[dex2];
intArray[dex2] = temp;
}
public static int partitionIt(int[] intArray, int left, int right, double pivot) {
int leftPtr = left;
int rightPtr = right - 1;
while (true) {
while (intArray[++leftPtr] < pivot)
;
while (intArray[--rightPtr] > pivot)
;
if (leftPtr >= rightPtr)
break;
else
swap(intArray, leftPtr, rightPtr);
}
swap(intArray, leftPtr, right - 1);
return leftPtr;
}
public static void manualSort(int[] intArray, int left, int right) {
int size = right - left + 1;
if (size <= 1)
return;
if (size == 2) {
if (intArray[left] > intArray[right])
swap(intArray, left, right);
return;
} else {
if (intArray[left] > intArray[right - 1])
swap(intArray, left, right - 1);
if (intArray[left] > intArray[right])
swap(intArray, left, right);
if (intArray[right - 1] > intArray[right])
swap(intArray, right - 1, right);
}
}
}
1 9 2 8 3 7 4 6 5 1 2 3 4 5 6 7 8 9
Selection Sort
public class MainClass {
public static void main(String[] args) {
int[] intArray = new int[] { 2, 6, 3, 8, 4, 9, 1 };
for (int i : intArray) {
System.out.print(i);
}
System.out.println();
selectionSort(intArray);
for (int i : intArray) {
System.out.print(i);
}
}
public static void selectionSort(int[] intArray) {
for (int out = 0; out < intArray.length - 1; out++) {
int min = out;
for (int in = out + 1; in < intArray.length; in++)
if (intArray[in] < intArray[min])
min = in;
swap(intArray, out, min);
}
}
private static void swap(int[] intArray, int one, int two) {
int temp = intArray[one];
intArray[one] = intArray[two];
intArray[two] = temp;
}
}
2638491 1234689
Shellsort
public class MainClass {
public static void main(String[] args) {
int[] intArray = { 1, 9, 2, 8, 3, 7, 4, 6, 5 };
for (int i : intArray) {
System.out.println(i);
}
shellSort(intArray);
System.out.println("after: ");
for (int i : intArray) {
System.out.println(i);
}
}
public static void shellSort(int[] intArray) {
int inner, outer;
int temp;
int h = 1;
while (h <= intArray.length / 3){
h = h * 3 + 1;
}
while (h > 0) {
for (outer = h; outer < intArray.length; outer++) {
temp = intArray[outer];
inner = outer;
while (inner > h - 1 && intArray[inner - h] >= temp) {
intArray[inner] = intArray[inner - h];
inner -= h;
}
intArray[inner] = temp;
}
h = (h - 1) / 3;
}
}
}
1 9 2 8 3 7 4 6 5 after: 1 2 3 4 5 6 7 8 9
Sorting Objects using insertion sort
class Person {
private String lastName;
private String firstName;
private int age;
public Person(String last, String first, int a) {
lastName = last;
firstName = first;
age = a;
}
public String toString() {
return "Last name: " + lastName + " First name: " + firstName + " Age: " + age;
}
public String getLast() {
return lastName;
}
}
public class MainClass {
public static void main(String[] args) {
Person[] persons = new Person[] {
new Person("a", "b", 23),
new Person("i", "a", 25),
new Person("y", "h", 26),
new Person("d", "e", 27) };
System.out.println("Before sorting:");
for (Person p : persons) {
System.out.println(p);
}
insertionSort(persons);
System.out.println("After sorting:");
for (Person p : persons) {
System.out.println(p);
}
}
public static void insertionSort(Person[] persons) {
int in, out;
for (out = 1; out < persons.length; out++) {
Person temp = persons[out];
in = out;
while (in > 0 && persons[in - 1].getLast().rupareTo(temp.getLast()) > 0) {
persons[in] = persons[in - 1];
--in;
}
persons[in] = temp;
}
}
}
Last name: y First name: h Age: 26 Last name: d First name: e Age: 27 After sorting: Last name: a First name: b Age: 23 Last name: d First name: e Age: 27 Last name: i First name: a Age: 25 Last name: y First name: h Age: 26