排序
1. 选择排序
package Ds.Sort.selectSort;
import java.util.Arrays;
public class selectSort {
public static void main(String[] args) {
int arr[] = new int[50];
for(int i =0 ;i<50;i++){
arr[i] = (int)(Math.random()*50);
}
System.out.println("排序前:");
System.out.println(Arrays.toString(arr));
selectSort(arr);
System.out.println("排序后:");
System.out.println(Arrays.toString(arr));
}
public static void selectSort(int[] arr){
for(int i =0 ;i<arr.length;i++){
int minIndex = i;
int min = arr[i];
for(int j =i+1;j<arr.length;j++){
if(arr[j]<min){
min =arr[j];
minIndex = j;
}
}
if(minIndex!=i){
arr[minIndex] =arr[i];
arr[i] = min;
}
}
}
}
2. 快速排序
package Ds.Sort.QuickSort;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = {75,67,35,27,35,89,50,98,100};
System.out.print("排序前:" + Arrays.toString(arr));
quickSort(arr);
System.out.println();
System.out.println("排序后:" + Arrays.toString(arr));
}
public static void quickSort(int[] arr) {
int low = 0;
int height = arr.length - 1;
quickSort(arr, low, height);
}
private static void quickSort(int[] arr, int low, int height) {
if (low < height) {
int index = partition(arr, low, height);
quickSort(arr, low, index - 1);
quickSort(arr, index + 1, height);
}
}
private static int partition(int[] arr, int low, int height) {
int i = low;
int j = height;
int x = arr[low];
while (i < j) {
while (arr[j] > x && i < j) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (arr[i] <= x && i < j) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = x;
return i;
}
}
3. 插入排序
package Ds.Sort.insertSort;
import java.util.Arrays;
public class insetSort {
public static void main(String[] args) {
int[] arr = {101,34,119,1};
System.out.println(Arrays.toString(arr));
insertSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void insertSort(int[] arr){
int insertVal=0;
int insertIndex=0;
for(int i =1;i<arr.length;i++){
insertVal = arr[i];
insertIndex = i-1;
while (insertIndex>=0 && insertVal<arr[insertIndex]){
arr[insertIndex+1] = arr[insertIndex];
insertIndex--;
}
if(insertIndex+1!=i)
arr[insertIndex+1] = insertVal;
}
}
}
4. 冒泡排序
package Ds.Sort.bubbleSort;
import java.util.Date;
public class bubbleSort {
public static void main(String[] args) {
int arr[] = new int[80];
for(int i =0 ;i<80;i++){
arr[i] = (int)(Math.random()*50);
}
Date date = new Date();
System.out.println(date);
bubbleSort(arr);
System.out.println(date);
}
public static int[] bubbleSort(int[] arr){
for (int i=0;i<arr.length-1;i++){
for (int j=0;j<arr.length-1-i;j++){
if (arr[j]>arr[j+1]){
int temp=arr[j];
arr[j] = arr[j+1];
arr[j+1]= temp;
}
}
}
return arr;
}
}
5. 希尔排序
package Ds.Sort.shellSort;
import java.util.Arrays;
public class shellSort {
public static void main(String[] args) {
int[] arr = {2, 5, 0, 6, 3, 9, 4, 8, 1, 7};
System.out.println(Arrays.toString(arr));
shellSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void shellSort(int[] arr) {
int tmp =0 ;
for(int gap =arr.length/2;gap>0;gap/=2){
for(int i = gap;i<arr.length;i++){
for(int j = i-gap ;j>=0;j -=gap){
if(arr[j]>arr[j+gap]){
tmp = arr[j];
arr[j] = arr[j+gap];
arr[j+gap] = tmp;
}
}
}
}
}
public static void shellSort2(int[] arr){
for(int gap = arr.length/2;gap>0;gap/=2){
for (int i=gap ;i< arr.length;i++){
int j =i ;
int temp = arr[i];
if(arr[j] <arr[j-gap]){
while (j-gap>=0 && temp<arr[j-gap]){
arr[j] = arr[j-gap];
j=j-gap;
}
arr[j] = temp;
}
}
}
}
}
6. 堆排序
package Ds.Sort.heapSort;
import java.util.Arrays;
public class heapSort {
public static void main(String[] args) {
int[] arr = {8,2,5,3,4,6,9,7,0,1};
System.out.println(Arrays.toString(arr));
heapSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void heapSort(int[] arr){
for(int i =arr.length/2-1;i>=0;i--){
adjustDown(arr,i,arr.length);
}
for (int j=arr.length-1;j>0 ;j--){
int temp = arr[0];
arr[0] = arr[j];
arr[j]= temp;
adjustDown(arr,0,j);
}
}
private static void adjustDown(int[] arr,int i ,int length){
int child=0;
int temp = 0;
for(temp = arr[i] ;i*2+1<length;i=child){
child = 2*i+1;
if (child!=length-1 && arr[child]<arr[child+1]){
child++;
}
if(temp<arr[child]){
arr[i] = arr[child];
}else break;
}
arr[i] = temp;
}
}
7.归并排序
package SortTest;
import java.util.Arrays;
public class margetSort {
public static void main(String[] args) {
int[] arr = {1, 6, 3, 8, 4, 9, 2, 5, 7};
int[] temp = new int[arr.length];
System.out.println(Arrays.toString(arr));
margetSort(arr, 0, arr.length - 1, temp);
System.out.println(Arrays.toString(arr));
}
public static void margetSort(int[] arr,int left ,int right ,int[] temp){
if (left<right){
int mid = (left+right)/2;
margetSort(arr,left,mid,temp);
margetSort(arr,mid+1,right,temp);
marge(arr,left,mid,right,temp);
}
}
private static void marge(int[] arr,int left,int mid,int right,int[] temp){
int i =left;
int j = mid+1;
int t =0;
while (i<=mid && j<=right){
if (arr[i]>= arr[j]){
temp[t++] = arr[j++];
}else {
temp[t++] = arr[i++];
}
}
while (i<=mid){
temp[t++] = arr[i++];
}
while (j<=right){
temp[t++] = arr[j++];
}
t =0;
int tempLeft = left;
while (tempLeft<=right){
arr[tempLeft++] = temp[t++];
}
}
}