package Sort_Zhu;
/*
冒泡排序:从第一个数开始,依次与后面的数进行比较,依次找到最大的
时间复杂度:O(n^2)
eg:{3,52,15,62,9,32}
第一次冒泡:3,15,52,9,32,62
第二次冒泡:3,15,9,32,52,62
第三次冒泡:3,9,15,32,52,62
第四次冒泡:没有数据交换,直接退出程序
*/
public class MpSort {
public static void main(String[] args) {
int[] a = new int[]{3,52,15,62,9,32};
mpsort(a,6);
for (int i = 0; i < a.length;i++){
System.out.println(a[i]);
}
}
public static void mpsort(int[] a, int n){
if (n == 1){
return;
}
for (int i = 0; i < n; i++){
boolean flag = false;
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;
flag = true;
}
}
if (!flag){
break;
}
}
}
}
package Sort_Zhu;
/*
插入排序:从第二个数开始,依次与前面的数进行比较,插入到合适的位置
时间复杂度:O(n^2)
eg:{6,52,3,12,35,91}
第一次插入:6,52,3,12,35,91(52 > 16不需要插入)
第二次插入:3,6,52,12,35,91
第三次插入:3,6,12,52,35,91
第四次插入:3,6,12,35,52,91
顺序已经正确,后面无需排序
*/
public class InsertSort {
public static void main(String[] args) {
int[] a = new int[]{6,52,3,12,35,91};
insertsort(a,6);
for (int i = 0; i < a.length;i++){
System.out.println(a[i]);
}
}
public static void insertsort(int[] a,int n){
if (n == 1){
return;
}
for (int i = 1; i < n; i++){
int tmp = a[i];
int j = i - 1;
for (;j >= 0; j--){
if (a[j] > tmp){
a[j+1] = a[j];
}else{
break;
}
}
a[j+1] = tmp;
}
}
}
package Sort_Zhu;
/*
选择排序:会从未排序区间中找到最小的值,放入已排序区间的末尾
时间复杂度:O(n^2)
eg:{3,52,6,16,4,9};
第一次选择:3,52,6,16,4,9
第二次选择:3,4,6,16,52,9
第三次选择:3,4,6,16,52,9
第四次选择:3,4,6,9,52,16
第五次选择:3,4,6,9,16,52
*/
public class SelectSort {
public static void main(String[] args) {
int[] a = new int[]{3,52,6,16,4,9};
selectsort(a,6);
for (int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
}
public static void selectsort(int[] a,int n){
if (n == 1){
return;
}
for (int i = 0; i < n; i++){
int min = a[i];
int k = i;
for (int j = i + 1;j < n; j++){
if (a[j] < min){
min = a[j];
k = j;
}
}
int tmp = a[i];
a[i] = min;
a[k] = tmp;
}
}
}
package Sort_Zhu;
/*
归并排序:利用递归的思想对两边的数组分别进行排序,然后比较两个有序数组并且合并
1)、稳定排序,相同值排序后还能保持前后关系
2)、非原地排序,需要额外申请O(n)的临时数组
3)、时间复杂度(On*logn)
*/
public class MergeSort {
public static void mergesort(int[] a,int left,int right,int[] tmp){
if (left < right){
//递归终止条件
int mid = (left + right) / 2;
mergesort(a,left,mid,tmp);
mergesort(a,mid+1,right,tmp);
sort(a,left,mid,right,tmp);
}
}
public static void sort(int[] a,int left,int mid,int right,int[] tmp){
int i = left;
int j = mid + 1;
int index = 0;
while (i <= mid && j <= right){
if (a[i] > a[j]){
tmp[index] = a[j];
index++;
j++;
}else{
tmp[index] = a[i];
index++;
i++;
}
}
while(i <= mid){
tmp[index] = a[i];
index++;
i++;
}
while(j <= right){
tmp[index] = a[j];
index++;
j++;
}
index = 0;
while (left <= right){
a[left] =tmp[index];
left++;
index++;
}
}
public static void main(String[] args) {
int[] a = new int[]{38,12,5,98,42,7,342,11,52,62,98,13,41,512,53,111};
int[] tmp = new int[a.length];
mergesort(a,0,a.length - 1,tmp);
for (int i = 0; i < a.length;i++){
System.out.println(a[i]);
}
}
}
package Sort_Zhu;
public class QuickSort {
public static void quicksort(int[] a,int p,int q){
if (p >= q){
return;
}
int r = partition(a,p,q);
quicksort(a,p,r-1);
quicksort(a,r+1,q);
}
public static int partition(int[] a,int start,int end){
int provt = a[end];
int i = start;
for (int j = start;j < end;j++){
if (a[j] < provt){
swap(a,i,j);
i += 1;
}
}
swap(a,end,i);
return i;
}
public static void swap(int[] a,int i,int j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static void main(String[] args) {
int[] a = new int[]{342,62,12,52,6,9,23,85,92,215};
quicksort(a,0,a.length - 1);
for (int i = 0; i < a.length;i++){
System.out.println(a[i]);
}
}
}