常见排序算法

    xiaoxiao2025-04-05  19

    不多说直接上代码,代码中实现了:快速排序、堆排序、直接插入排序、希尔排序

    package com.scalahome; /** * Created by Administrator on 2016/8/14. */ public class SortDemo { public static void main(String[] args) { int[] array = new int[]{2,24,4,53,2551,32,531,4,1,43,-24,2,53}; SortDemo sortDemo = new SortDemo(); sortDemo.printArray(array); // sortDemo.quickSort(array); // sortDemo.heapSort(array); // sortDemo.insertSort(array); sortDemo.shellSort(array); sortDemo.printArray(array); } /** * 希尔排序 * * @param array */ public void shellSort(int[] array) { for(int gap = array.length / 2; gap > 0; gap = gap / 2) { for(int offset = 0; offset < gap; offset++) { insertSort(array, gap, offset); } } } public void insertSort(int[] array, int gap, int offset) { for(int i = offset + gap; i < array.length; i += gap) { int j = i - gap; int newElement = array[i]; for(; j >= 0 && array[j] > newElement; j -= gap) { array[j + gap] = array[j]; } array[j + gap] = newElement; } } /** * 直接插入排序 * * @param array */ public void insertSort(int[] array) { for(int i = 1; i < array.length; i++) { int j = i - 1; int newElement = array[i]; for(; j >= 0 && array[j] > newElement; j--) { array[j + 1] = array[j]; } array[j + 1] = newElement; } } /** * 堆排序 * * @param array */ public void heapSort(int[] array) { for(int i = array.length - 1; i > 0; i--) { maxHeap(array, i); swap(array, 0, i); } } public void maxHeap(int[] array, int lastPosition) { for(int i = lastPosition / 2; i >= 0; i--) { int j = i; while (2 * j + 1 <= lastPosition) { int firstChildIndex = 2 * j + 1; int secondChildIndex = firstChildIndex + 1; int bigger = firstChildIndex; if(secondChildIndex < lastPosition && array[secondChildIndex] > array[firstChildIndex]) { bigger = secondChildIndex; } if(array[bigger] > array[j]) { swap(array, j, bigger); j = bigger; } else { break; } } } } /** * 快速排序 * * @param array */ public void quickSort(int[] array) { quickSort(array, 0, array.length - 1); } public void quickSort(int[] array, int start, int to) { if(start >= to) return; int position = partitionSort(array, start, to); quickSort(array, start, position); quickSort(array, position + 1, to); } public int partitionSort(int[] array, int from, int to) { int i = from; int j = to; int value = array[i]; int flag = 0; while(j > i) { if(flag == 0) { if(array[j] < value) { swap(array, i, j); flag = (flag + 1) % 2; } else { j--; } } else { if(array[i] > value) { swap(array, i, j); flag = (flag + 1) % 2; } else { i++; } } } return i; } public void swap(int[] array, int pos1, int pos2) { array[pos1] = array[pos1] ^ array[pos2]; array[pos2] = array[pos1] ^ array[pos2]; array[pos1] = array[pos1] ^ array[pos2]; } public void printArray(int[] array) { StringBuilder buffer = new StringBuilder(); buffer.append('['); for(int item : array) { buffer.append(item); buffer.append(','); } buffer.setLength(buffer.length() - 1); buffer.append(']'); System.out.println(buffer.toString()); } }

    转载请注明原文地址: https://ju.6miu.com/read-1297765.html
    最新回复(0)