1. 直接插入- 2. 希尔排序
3. 选择——- 4. 冒泡
5. 快速——–6.堆
- 直接插入
直接插入排序法的排序原则是:将一组无序的数字排列成一排,左端第一个数字为已经完成排序的数字,其他数字为未排序的数字。然后从左到右依次将未排序的数字插入到已排序的数字中。
public class InsertSort {
public void insertSort(int[] array, int first, int last) {
int temp, i, j;
for (i = first + 1; i <= last - 1; i++) {// 默认以第一个数为有序序列,后面的数为要插入的数。
temp = array[i];
j = i - 1;
while (j >= first && array[j] > temp) {// 从后进行搜索如果搜索到的数小于temp则该数后移继续搜索,直到搜索到小于或等于temp的数即可
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
// 打印每次排序结果
for (int m = 0; m <= array.length - 1; m++) {
System.out.print(array[m] + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
InsertSort insertSort = new InsertSort();
int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
insertSort.insertSort(array, 0, array.length);// 注意此处是0-9而不是0-8
for (int i = 0; i <= array.length - 1; i++) {
System.out.print(array[i] + "\t");
}
}
}
- shell排序
希尔排序又称“缩小增量排序”,该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序(增量足够小)时,再对全体元素进行一次直接插入排序。因为直接插入排序在元素基本有序的情况下(接近最好情况),效率是很高的,因此希尔排序在时间效率上比前两种方法有较大提高。
public class ShellSort {
public void shellSort(int[] array, int n) {
int i, j, gap;
int temp;
for (gap = n / 2; gap > 0; gap /= 2) {// 计算gap大小
for (i = gap; i < n; i++) {// 将数据进行分组
for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j -= gap) {// 对每组数据进行插入排序
temp = array[j];
array[j] = array[j + gap];
array[j + gap] = temp;
}
// 打印每趟排序结果
for (int m = 0; m <= array.length - 1; m++) {
System.out.print(array[m] + "\t");
}
System.out.println();
}
}
}
public static void main(String[] args) {
ShellSort shellSort = new ShellSort();
int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
shellSort.shellSort(array, array.length);// 注意为数组的个数
for (int m = 0; m <= array.length - 1; m++) {
System.out.print(array[m] + "\t");
}
}
}
- 选择排序
选择排序的第一趟处理是从数据序列所有n个数据中选择一个最小的数据作为有序序列中的第1个元素并将它定位在第一号存储位置,第二趟处理从数据序列的n-1个数据中选择一个第二小的元素作为有序序列中的第2个元素并将它定位在第二号存储位置,依此类推,当第n-1趟处理从数据序列的剩下的2个元素中选择一个较小的元素作为有序序列中的最后第2个元素并将它定位在倒数第二号存储位置,至此,整个的排序处理过程就已完成。
public class SelectionSort {
public void selectionSort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j <= array.length - 1; j++) {// 第i个和第j个比较j可以取到最后一位,所以要用j<=array.length-1
if (array[i] > array[j]) {// 注意和冒泡排序的区别,这里是i和j比较。
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// 打印每趟排序结果
for (int m = 0; m <= array.length - 1; m++) {
System.out.print(array[m] + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
SelectionSort selectionSort = new SelectionSort();
int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
selectionSort.selectionSort(array);
for (int m = 0; m <= array.length - 1; m++) {
System.out.print(array[m] + "\t");
}
}
}
- 冒泡排序
在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
void bubbleSort(int a[], int n){
for(int i =0 ; i< n-1; ++i) {
for(int j = 0; j < n-i-1; ++j) {
if(a[j] > a[j+1])
{
int tmp = a[j] ; a[j] = a[j+1] ; a[j+1] = tmp;
}
}
}
}
- 快速排序
快速排序(Quicksort)是对冒泡排序的一种改进。由C. A. R.Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
public class QuickSort {
public int partition(int[] sortArray, int low, int height) {
int key = sortArray[low];// 刚开始以第一个数为标志数据
while (low < height) {
while (low < height && sortArray[height] >= key)
height--;// 从后面开始找,找到比key值小的数为止
sortArray[low] = sortArray[height];// 将该数放到key值的左边
while (low < height && sortArray[low] <= key)
low++;// 从前面开始找,找到比key值大的数为止
sortArray[height] = sortArray[low];// 将该数放到key值的右边
}
sortArray[low] = key;// 把key值填充到low位置,下次重新找key值
// 打印每次排序结果
for (int i = 0; i <= sortArray.length - 1; i++) {
System.out.print(sortArray[i] + "\t");
}
System.out.println();
return low;
}
public void sort(int[] sortArray, int low, int height) {
if (low < height) {
int result = partition(sortArray, low, height);
sort(sortArray, low, result - 1);
sort(sortArray, result + 1, height);
}
}
public static void main(String[] args) {
QuickSort quickSort = new QuickSort();
int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
for (int i = 0; i <= array.length - 1; i++) {
System.out.print(array[i] + "\t");
}
System.out.println();
quickSort.sort(array, 0, 8);
for (int i = 0; i <= array.length - 1; i++) {
System.out.print(array[i] + "\t");
}
}
}
- 堆排序
void print(int a[], int n){
for(int j= 0; j<n; j++){
cout<<a[j] <<" ";
}
cout<<endl;
}
/**
* 已知H[s…m]除了H[s] 外均满足堆的定义
* 调整H[s],使其成为大顶堆.即将对第s个结点为根的子树筛选,
*
* @param H是待调整的堆数组
* @param s是待调整的数组元素的位置
* @param length是数组的长度
*
*/
void HeapAdjust(int H[],int s, int length)
{
int tmp = H[s];
int child = 2*s+1; //左孩子结点的位置。(i+1 为当前调整结点的右孩子结点的位置)
while (child < length) {
if(child+1 <length && H[child]<H[child+1]) { // 如果右孩子大于左孩子(找到比当前待调整结点大的孩子结点)
++child ;
}
if(H[s]<H[child]) { // 如果较大的子结点大于父结点
H[s] = H[child]; // 那么把较大的子结点往上移动,替换它的父结点
s = child; // 重新设置s ,即待调整的下一个结点的位置
child = 2*s+1;
} else { // 如果当前待调整结点大于它的左右孩子,则不需要调整,直接退出
break;
}
H[s] = tmp; // 当前待调整的结点放到比其大的孩子结点位置上
}
print(H,length);
}
/**
* 初始堆进行调整
* 将H[0..length-1]建成堆
* 调整完之后第一个元素是序列的最小的元素
*/
void BuildingHeap(int H[], int length)
{
//最后一个有孩子的节点的位置 i= (length -1) / 2
for (int i = (length -1) / 2 ; i >= 0; --i)
HeapAdjust(H,i,length);
}
/**
* 堆排序算法
*/
void HeapSort(int H[],int length)
{
//初始堆
BuildingHeap(H, length);
//从最后一个元素开始对序列进行调整
for (int i = length - 1; i > 0; --i)
{
//交换堆顶元素H[0]和堆中最后一个元素
int temp = H[i]; H[i] = H[0]; H[0] = temp;
//每次交换堆顶元素和堆中最后一个元素之后,都要对堆进行调整
HeapAdjust(H,0,i);
}
}
int main(){
int H[10] = {3,1,5,7,2,4,9,6,10,8};
cout<<"初始值:";
print(H,10);
HeapSort(H,10);
//selectSort(a, 8);
cout<<"结果:";
print(H,10);
}