选择排序+插入排序+冒泡排序+快速排序+归并排序
package javaFoundations;
/*
* Contact.java
* Represents a phone contact that implements Comparable
*/
public class Contact implements Comparable{
private String firstname, lastname, phone;
public Contact(String first, String last, String telephone){
this.firstname = first;
this.lastname = last;
this.phone = telephone;
}
/*
* returns a string representation of this contact.
*/
public String toString(){
return lastname + ", " + firstname + ": " + phone;
}
/*
* use both last and first names to determine lexical ordering.
*/
public int compareTo(Object other){
int result;
if(lastname.equals(((Contact)other).lastname))
result = firstname.compareTo(((Contact)other).firstname);
else
result = lastname.compareTo(((Contact)other).lastname);
return result;
}
}
package javaFoundations;
/*
* SortPlayerList.java
* Demonstrates a series of sort of Comparable objests
*/
public class SortPlayerList {
public static void main(String[] args) {
Contact[] players = new Contact[7];
players[0] = new Contact("Rodger", "Federer", "222-222-2222");
players[1] = new Contact("Andy", "Roddic", "444-444-4444");
players[2] = new Contact("Maria", "Sharapove", "555-555-5555");
players[3] = new Contact("Venus", "Williams", "777-777-7777");
players[4] = new Contact("Lleyton", "Hewitt", "333-333-3333");
players[5] = new Contact("Eleni", "Daniilidou", "111-111-1111");
players[6] = new Contact("Serena", "Williams", "666-666-6666");
Sorting.quickSort(players, 0, 6);
for(Comparable player : players)
System.out.println(player);
}
}
package javaFoundations;
/*
* Sorting.java
* Contains various sort algorithms that operate on an array of
* Comparable objests.
*/
public class Sorting {
//--------------------------------------------------------------------------------
/*
* Sorts the specified array of integers using the selection
* sort algorithm.
*/
public static void selectionSort(Comparable[] data){
int min;
for(int index = 0; index < data.length-1; index++){
min = index;
for(int scan = index+1; scan < data.length; scan++){
if(data[scan].compareTo(data[min]) < 0 )
min = scan;
}
swap(data, min, index);
}
}
private static void swap(Comparable[] data, int index1, int index2){
Comparable temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
//---------------------------------------------------------------------------------
/*
* Sorts the specified array of objects using an insertion
* sort algorithm.
*/
public static void insertionSort(Comparable[] data){
for(int i = 1; i < data.length; i++){
Comparable key = data[i];
int position = i;
while(position > 0 && data[position - 1].compareTo(key) > 0 ){
data[position] = data[position - 1];
position--;
}
data[position] = key;
}
}
//----------------------------------------------------------------------------------
/*
* Sorts the specified array of objects using a bubble sort
* algorithm
*/
public static void bubbleSort(Comparable[] data) {
for(int i = data.length - 1; i > 0; i--){
for(int j = 0; j < i ; j++){
if(data[j].compareTo(data[j+1]) > 0)
swap(data, j, j + 1);
}
}
}
//----------------------------------------------------------------------------------
/*
* Sorts the specified array of objects using the quick sort
* algorithm
*/
public static void quickSort(Comparable[] data, int min, int max){
int pivot;
if(min < max){
pivot = partition(data, min, max); // make partitions
quickSort(data, min, pivot - 1);
quickSort(data, pivot + 1, max);
}
}
// Creates the partitions need for the quick sort
public static int partition(Comparable[] data, int min, int max){
// Use the first element as the partition value
Comparable partitionValue = data[min];
int left = min;
int right = max;
while(left < right){
//Search for an element the is > the partition element
while(data[left].compareTo(partitionValue) <= 0 && left < right)
left++;
//Search for an element the is < the partition element
while(data[right].compareTo(partitionValue) > 0)
right--;
if(left < right)
swap(data, left, right);
}
// Move the partition element to its final position
swap(data, min, right);
return right;
}
//------------------------------------------------------------------------------------
/*
* Sorts the specified array of objects using the merge sort
* algorithm.
*/
public static void mergeSort(Comparable[] data, int min, int max){
if(min < max){
int mid = (min + max)/2;
mergeSort(data, min, mid);
mergeSort(data, mid + 1, max);
merge(data, min, mid, max);
}
}
public static void merge(Comparable[] data, int first, int mid, int last){
Comparable[] temp = new Comparable[data.length];
int first1 = first, last1 = mid; // endpoints of first subarray
int first2 = mid + 1, last2 = last; // endpoints of second subarray
int index = first1; // nest index open in temp array
while(first1 <= last1 && first2 <= last2){
if(data[first1].compareTo(data[first2]) < 0){
temp[index] = data[first1];
first1++;
}
else{
temp[index] = data[first2];
first2++;
}
index++;
}
// Copy remaining elements from first subarray, if any
while(first1 <= last1)
temp[index++] = data[first1++];
// Copy remaining elements from second subarray, if any
while(first2 <= last2)
temp[index++] = data[first2++];
// Copy merged data into orginal array
for(index = first; index <= last; index++)
data[index] = temp[index];
}
}
output:
Daniilidou, Eleni: 111-111-1111
Federer, Rodger: 222-222-2222
Hewitt, Lleyton: 333-333-3333
Roddic, Andy: 444-444-4444
Sharapove, Maria: 555-555-5555
Williams, Serena: 666-666-6666
Williams, Venus: 777-777-7777