package first;
import java.util.*;
public class DataStructureSort {
public DataStructureSort() {
}
public DataStructureSort(String name) {
}
/**
* 选择排序
*
* @param a
*/
public void choiceSort(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[i]) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
/**
* 冒泡排序
*
* @param a
*/
public void bubbleSort(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] < a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
/**
* 直接插入排序,属于稳定的排序,时间复杂性为O(n2),空间复杂度为O(1),所谓稳定的排序,指的是若要排序的数字中,有两个相同的数字,排序前后,两者的先后顺序不变
* 基本思路:每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序
*
* @param args
*/
public void insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i - 1]) {
int j = i - 1;
int temp = a[i];
do {
a[j + 1] = a[j];
} while (--j >= 0 && temp < a[j]);
a[j + 1] = temp;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
/**
* 快速排序,里面包含递归
*
* @param a
* @param low
* @param high
*/
public void quicklySort(int[] a, int low, int high) {
int pivot;
if (low < high) {
pivot = partition(a, low, high);// pivot标记最大数的下标,以最大数的下标作为分界
System.out.println("pivot : " + pivot);
quicklySort(a, low, pivot - 1);
quicklySort(a, pivot + 1, high);
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
/**
* 快速排序找分界点
*
* @param a
* @param low
* @param high
* @return
*/
public int partition(int a[], int low, int high) {
int pivot, p_pos, i;
p_pos = low;
pivot = a[p_pos];
for (i = low + 1; i <= high; i++) {
if (a[i] < pivot) {
p_pos++;
swap(a, p_pos, i);
}
}
swap(a, low, p_pos);
return p_pos;
}
/**
* 两个数交换
*
* @param a
* @param i
* @param j
*/
public void swap(int a[], int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
/**
* 快速排序中,找出分界点(是快速排序最难的地方),这一点搞定,就搞定快速排序了
*
* @param a
* @param low
* @param high
*/
public void indexOfLargest(int[] a, int low, int high) {
int pivot, p_pos, i;
p_pos = low;
pivot = a[p_pos];
for (int j = 0; j < a.length; j++) {
System.out.print(" " + a[j]);
}
System.out.println();
for (i = low + 1; i < a.length; i++) {
if (a[i] < pivot) {
p_pos++;
swap(a, p_pos, i);
}
}
swap(a, low, p_pos);
System.out.println("第一次分界点的下标 : " + p_pos);
for (int j = 0; j < a.length; j++) {
System.out.print(" " + a[j]);
}
}
public static void main(String[] args) {
DataStructureSort ds = new DataStructureSort();
int[] b = new int[] { 23, 38, 35, 1, -1, 20, 1000, 40, 100 };
// 调冒泡
// ds.bubbleSort(b);
// 调选择
// ds.choiceSort(b);
// 调插入排序
// ds.insertSortSelf(b);
// ds.quicklySort(b, 0, b.length - 1);
ds.indexOfLargest(b, 0, b.length);
}
}
说明:
一.排序
1.直接插入排序
38, 35, 20, 18, 40
对于该序列,自己的描述(按升序):
(1)35与38比较,35小于38,交换,本次循环结束;35,38,20,18,40
(2)20与38比较,20小于38,交换35,20,38,18,40;20与35比较,20小于35,交换,20,35,38,18,40
(3)同上,首先18与38比较, 序列变化依次为:20,35,18,38,40;20,18,35,38,40;18,20,35,38,40
(4)同上,首先40与38比较,大于,直接结束,挺经典
2.快速排序
38, 35, 20, 18, 40, -1, 100(升序排序)
传入方法的参数为数组,及要排序的数字范围的下标。起始下标要小于终止下标。
若low<high,则递归调用,第一次循环以38为分界线,将大于38的数放在其左边,小于38的数放在其右边,找出分界点的下标(最难,最关键,也最重要),继续递归调用
二.排序的分类
1.插入排序,分为直接插入排序和希尔排序
2.选择排序,分为直接选择排序和堆排序
3.交换排序,分为快速排序和冒泡排序
4.归并排序
5.分配排序,分为箱排序和基数排序