//********************************************************************
// Sorting.java Java Foundations
//
// Contains various sort algorithms that operate on an array of
// Comparable objects.
//********************************************************************
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++) //选出index+1。。。之后最小的与当前的min交换
if (data[scan].compareTo(data[min]) < 0)
min = scan;
swap(data, min, index);
}
}
// -----------------------------------------------------------------
// Swaps two elements in the specified array.
// -----------------------------------------------------------------
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 index = 1; index < data.length; index++) {
Comparable key = data[index]; //当前要插入的元素
int position = index;
// Shift larger values to the right
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) // 冒泡排序
{
int position, scan;
for (position = data.length - 1; position >= 0; position--) {
for (scan = 0; scan <= position - 1; scan++)
if (data[scan].compareTo(data[scan + 1]) > 0)
swap(data, scan, scan + 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); // sort left partition
quickSort(data, pivot + 1, max); // sort right partition
}
}
// -----------------------------------------------------------------
// Creates the partitions needed for quick sort.
// -----------------------------------------------------------------
private static int partition(Comparable[] data, int min, int max) {
// Use first element as the partition value
Comparable partitionValue = data[min];
int left = min;
int right = max;
while (left < right) {
// Search for an element that is > the partition element
while (data[left].compareTo(partitionValue) <= 0 && left < right) //只要小于中轴元素
left++;
// Search for an element that is < the partitionelement
while (data[right].compareTo(partitionValue) > 0)
right--;
if (left < right) //推出两个while,肯定是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);
}
}
// -----------------------------------------------------------------
// Sorts the specified array of objects using the merge sort
// algorithm.
// -----------------------------------------------------------------
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; // next index open in temp array
// Copy smaller item from each subarray into temp until one
// of the subarrays is exhausted
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];
first1++;
index++;
}
// Copy remaining elements from second subarray, if any
while (first2 <= last2) {
temp[index] = data[first2];
first2++;
index++;
}
// Copy merged data into original array
for (index = first; index <= last; index++)
data[index] = temp[index];
}
}
分享到:
相关推荐
JAVA排序大全 冒泡 快速 选择 归并排序
排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序.mht
JAVA实现选择,冒泡,归并,插入,快速排序。并随机生成不同规模的随机数来测试各种排序方法耗费的时间。
java快速排序 归并排序 冒泡排序 选择排序
JAVA排序算法: 直接插入,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序,包括算法的详细介绍,以及对几种算法的详细测试
快速排序、归并排序、希尔排序、冒泡排序、选择排序、插入排序等8中排序方式原理分析java实现
用java语言实现冒泡排序、插入排序、堆排序、快速排序、归并排序、希尔排序、桶排序,并且对各种排序算法进行性能的比较。
八种排序算法原理及Java实现( 冒泡排序+快速排序直接插入排序+希尔排序+选择排序+归并排序+基数排序)
用java实现了以下算法: 1、冒泡排序、冒泡排序的两种改进。 2、插入排序。 3、选择排序。 4、希尔排序。 5、归并排序。 6、快速排序。
Java代码-排序-直接插入排序、希尔排序、直接选择排序、冒泡排序、堆排序、快速排序、归并排序中部分排序以及算法-贪心法,大家可以将源码下载下来进行学习,附带着注释和解释,有不理解的可以找博主一起探讨,共同...
冒泡排序 3. 插入排序 4. 希尔排序 5. 堆排序 6. 归并排序 7. 快速排序 8. 基数排序 9. 计数排序 10. 桶排序 十种排序代码 我的博文地址:http://blog.csdn.net/u010156024/article/details/48932219
3种排序方法的对比(快速排序,归并排序,冒泡排序)
包括冒泡,归并和快速排序三种排序方式的java代码,可执行
快速排序
选择排序 冒泡排序 插入排序 希尔排序 堆排序 归并排序 快速排序 基数排序 计数排序 桶排序 我的博客地址:http://blog.csdn.net/u010156024/article/details/48932219
idea项目:一个主类选择调用6个排序类,记录了学习排序算法的过程,可以自己更改优化,每一种排序是一个类,有需要可以copy走,可重用性强
用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等。 插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm....
Java各种排序算法集合: 1)插入排序(直接插入排序、希尔排序) 2)交换排序(冒泡排序、快速排序) 3)选择排序(直接选择排序、堆排序) 4)归并排序 5)分配排序(箱排序、基数排序)
冒泡排序 简单选择排序 直接插入排序 希尔排序 归并排序 快速排序等排序方法,使用java详细代码 附注释,清晰明白
八大排序java实现版本,直接插入排序、折半插入排序、冒泡排序、简单选择排序、希尔插入排序、快速排序 、堆排序、2-路归并排序 、基数排序,并有时间比较,博文...