快排,希尔排序,归并排序递归和非递归实现,冒泡,选择,插入排序
import java.util.*;
public class Sort{
public static void main(String[] args){
int[] arr = {9,78,0,23,567,70,19,456};
//System.out.println(Arrays.toString(arr));
// QuickSort(arr,0,arr.length-1);
//shellSort(arr);
//margeSort(arr,0,arr.length-1,new int[arr.length]);
int[] arr2 = {98,56,42,53,46,12,29,23,59};
int[] arr3 = {98,56,42};
System.out.println(Arrays.toString(arr2));
//margeSort(arr2);
//bubbleSort(arr2);
//selectSort(arr2);
//insertSort(arr2);
insertSort1(arr2);
System.out.println(Arrays.toString(arr2));
}
/**
* 快排
* @param arr
* @param low
* @param high
*/
public static void QuickSort(int[] arr,int low, int high){
int start = low;
int end = high;
int temp = arr[low] ;
int key = arr[low];
while(start<end){
while(start<end && arr[end]>=key) end--;
if(arr[end]<=key) arr[start]=arr[end];
while(start<end && arr[start] <= key) start++;
if(arr[start] >= key) arr[end] = arr[start];
}
arr[start] = temp;
if(start>low) QuickSort(arr,low,start-1);
if(high > end) QuickSort(arr,end+1,high);
}
/**
* 希尔排序
*/
public static void shellSort(int[] arr){
int low = 0, high = arr.length;
for (int gap = high/2; gap > 0; gap/=2) {
for (int i = gap; i < high; i++) {
for (int j = i-gap; j >=0 ; j-=gap) {
if (arr[j]>arr[j+gap]){
int temp = arr[j];
arr[j] = arr[j+gap];
arr[j+gap] = temp;
}
}
}
}
}
/**
* 归并排序,递归实现
*/
public static void margeSort(int[] arr,int left,int right,int[] temp){
if (left<right){
int mid = (left+right)/2;
margeSort(arr, left, mid, temp);
margeSort(arr, mid+1, right, temp);
marge(arr,left,mid,right,temp);
}
}
public static void marge(int[]arr,int left,int mid,int right,int[] temp){
int leftL = left;
int rightL = mid+1;
int cur = 0;
while (leftL<=mid && rightL<=right){
if (arr[leftL]<=arr[rightL]){
temp[cur] = arr[leftL];
cur++;
leftL++;
}else {
temp[cur] = arr[rightL];
cur++;
rightL++;
}
}
while (leftL<=mid){
temp[cur] = arr[leftL];
cur++;
leftL++;
}
while (rightL<=right){
temp[cur] = arr[rightL];
cur++;
rightL++;
}
cur = 0;
int templeft= left;
while (templeft<=right){
arr[templeft]=temp[cur];
cur++;
templeft++;
}
}
/**
* 归并排序,非递归实现
*/
public static void margeSort(int[] arr){
int k =1;
int[] temp = new int[arr.length];
while (k<arr.length){
margePass(arr,temp,k,arr.length);
k=2*k;
}
}
public static void margePass(int[] arr,int[] temp,int s,int n){
int i = 0;
while (i <= n-2*s){
marge(arr,i,i+s-1,i+2*s-1,temp);
i=i+2*s;
}
if(i<=n-s){
marge(arr,i,i+s-1,n-1,temp);
}
}
/**
* 冒泡排序
*/
public static void bubbleSort(int[] arr){
boolean flag = true;
for (int i = 0; i < arr.length && flag; i++) {
flag=false;
for (int j = arr.length-2; j >= i; j--) {
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = true;
}
}
}
}
/**
* 简单选择排序
*/
public static void selectSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
int min = i;
for (int j = i+1; j < arr.length; j++) {
if (arr[min]>arr[j]){
min = j;
}
}
if (i!=min){
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
/**
* 直接插入排序
*/
public static void insertSort(int[] arr){
for (int i = 1; i < arr.length; i++) {
if (arr[i]<arr[i-1]){
int temp = arr[i];
int j;
for (j = i-1; j>=0 && arr[j]>temp; j--) {
arr[j+1]=arr[j];
}
arr[j+1] = temp;
}
}
}
/**
* 基数排序
* @param arr
*/
public static void myRadixSort(int[] arr){
//1,找数组中最大的数的位数
int maxNum = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i]>maxNum)
maxNum = arr[i];
}
int maxNumLength = 0;//最大数的位数
while (maxNum!=0){
maxNumLength++;
maxNum /= 10;
}
int[][] bucket = new int[10][arr.length];//10个桶
int[] bucketElementCount = new int[10];
for (int i = 0, n = 1; i < maxNumLength; i++,n*=10) {
for (int value : arr) {
int digitOfElement = value / n % 10;
bucket[digitOfElement][bucketElementCount[digitOfElement]] = value;
bucketElementCount[digitOfElement]++;
}
int index = 0;
for (int j = 0; j < bucketElementCount.length; j++) {
if (bucketElementCount[j]!=0){
for (int k = 0; k < bucketElementCount[j]; k++) {
arr[index++] = bucket[j][k];
}
}
bucketElementCount[j]=0;
}
}
}
}
2020年6月25日,端午;
添加几个链表的排序:
链表归并排序,递归实现。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode fast = head.next;
ListNode slow = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
ListNode temp = slow.next;
slow.next = null;
ListNode left = sortList(head);
ListNode right = sortList(temp);
ListNode helper = new ListNode(0);
ListNode res = helper;
while(left!=null && right != null){
if(left.val<right.val){
helper.next = left;
left = left.next;
}else{
helper.next = right;
right = right.next;
}
helper = helper.next;
}
helper.next = left!=null ? left : right;
return res.next;
}
}
链表插入排序:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode dummy = new ListNode();
dummy.next = head;
ListNode pre = head;
ListNode cur = head.next;
while(cur!=null){
if(pre.val<=cur.val){
pre = cur;
cur = cur.next;
}else{
ListNode p = dummy;
while(p.next != cur && p.next.val < cur.val){
p = p.next;
}
pre.next = cur.next;
cur.next = p.next;
p.next = cur;
cur = pre.next;
}
}
return dummy.next;
}
}