提供了以下排序:
冒泡排序選擇排序插入排序希爾排序快速排序歸并排序桶排序堆排序?
?
package com.xingej.algorithm.sort;import java.util.ArrayList;
import java.util.Collections;
public class SortUtils {
private SortUtils() {}
public static void bubbleSort(int[] arr) {
int temp;
for (
int i =
0; i < arr.length -
1; i++) {
for (
int j = arr.length -
1; j > i; j--) {
if (arr[j] < arr[j -
1]) {temp = arr[j];arr[j] = arr[j -
1];arr[j -
1] = temp;}}}}
public static void selectSort(int[] arr) {
int minIndex =
0;
int value;
for (
int i =
0; i < arr.length -
1; i++) {minIndex = i;
value = arr[minIndex];
for (
int j = i +
1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {minIndex = j;}}arr[i] = arr[minIndex];arr[minIndex] =
value;}}
public static void insertSort(int[] arr) {
for (
int right =
1; right < arr.length; right++) {
int temp = arr[right];
int left = right -
1;
while (left >=
0 && arr[left] > temp) {arr[left +
1] = arr[left];left--;}arr[left +
1] = temp;}}
public static void shellSort(int[] arr) {
int left;
int right;
int temp;
int h =
1;
while (h < arr.length /
3) {h =
3 * h +
1;}
while (h >
0) {
for (right = h; right < arr.length; right++) {temp = arr[right]; left = right;
while (left > h -
1 && arr[left - h] >= temp) {arr[left] = arr[left - h];left = left - h;}arr[left] = temp;}h = (h -
1) /
3;}}
public static void quickSort(int arr[]) {recQuickSort(arr,
0, arr.length -
1);}
private static void recQuickSort(int arr[], int left, int right) {
int size = right - left +
1;
if (size <
10) {insertSort(arr, left, right);}
else {
int median = medianof3(arr, left, right);
int partition = partitionIt(arr, left, right, median);recQuickSort(arr, left, partition -
1);recQuickSort(arr, partition +
1, right);}}
private static int partitionIt(int arr[], int left, int right, int pivot) {
int leftPtr = left;
int rightPtr = right -
1;
while (
true) {
while (arr[++leftPtr] < pivot);
while (arr[--rightPtr] > pivot);
if (leftPtr >= rightPtr) {
break; }
else {swap(arr, leftPtr, rightPtr); }}swap(arr, leftPtr, right -
1);
return leftPtr;}
private static int medianof3(int arr[], int left, int right) {
int centerIndex = (left + right) /
2;
if (arr[left] > arr[centerIndex]) {swap(arr, left, centerIndex);}
if (arr[left] > arr[right]) {swap(arr, left, right);}
if (arr[centerIndex] > arr[right]) {swap(arr, centerIndex, right);}swap(arr, centerIndex, right -
1);
return arr[right -
1];}
private static void insertSort(int arr[], int left, int right) {
int in,
out;
int temp;
for (
out = left +
1;
out <= right;
out++) {temp = arr[
out];
in =
out;
while (
in > left && arr[
in -
1] >= temp) {arr[
in] = arr[
in -
1];
in--;}arr[
in] = temp;}}
private static void swap(int arr[], int left, int right) {
int temp = arr[left];arr[left] = arr[right];arr[right] = temp;}
public static void mergeSort(int arr[]) {
int[] workSpace =
new int[arr.length];recMergeSort(arr, workSpace,
0, arr.length -
1);}
private static void recMergeSort(int arr[], int workSpace[], int lowerBound, int upperBound) {
if (lowerBound == upperBound) {
return;}
else {
int mid = (lowerBound + upperBound) /
2;recMergeSort(arr, workSpace, lowerBound, mid);recMergeSort(arr, workSpace, mid +
1, upperBound);merge(arr, workSpace, lowerBound, mid +
1, upperBound);}}
private static void merge(int arr[], int workSpace[], int lowPtr, int highPtr, int upperBound) {
int i =
0;
int lowerBound = lowPtr;
int mid = highPtr -
1;
int n = upperBound - lowerBound +
1;
while (lowPtr <= mid && highPtr <= upperBound) {
if (arr[lowPtr] < arr[highPtr]) {workSpace[i++] = arr[lowPtr++];}
else {workSpace[i++] = arr[highPtr++];}}
while (lowPtr <= mid) {workSpace[i++] = arr[lowPtr++];}
while (highPtr <= upperBound) {workSpace[i++] = arr[highPtr++];}
for (i =
0; i < n; i++) {arr[lowerBound + i] = workSpace[i];}}
public static void bucketSort(int arr[]) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (
int i =
0; i < arr.length; i++) {max = Math.max(max, arr[i]);min = Math.min(min, arr[i]);}
int bucketNum = (max - min) / (arr.length) +
1;ArrayList<ArrayList<Integer>> bucketArr =
new ArrayList<>();
for (
int i =
0; i < bucketNum; i++) {bucketArr.
add(
new ArrayList<>());}
for (
int i =
0; i < arr.length; i++) {
int bucketIndex = (arr[i] - min) / (arr.length);bucketArr.
get(bucketIndex).
add(arr[i]);}
for (
int i =
0; i < bucketArr.size(); i++) {Collections.sort(bucketArr.
get(i));}
int j =
0;
for (
int i =
0; i < bucketArr.size(); i++) {ArrayList<Integer> arrayList = bucketArr.
get(i);
for (Integer key : arrayList) {arr[j++] = key;}}}
public static void heapSort(int arr[]) {
int size = arr.length;Heap heap =
new Heap(size);
for (
int i =
0; i < size; i++) {Node node =
new Node(arr[i]);heap.insertAt(i, node);heap.incrementSize();}
for (
int j = size /
2 -
1; j >=
0; j--) {heap.trickleDown(j);}
for (
int j = size -
1; j >=
0; j--) {Node biggestNode = heap.
remove();heap.insertAt(j, biggestNode);}System.
out.println(
"----打印排序后的堆----");heap.displayArray();}
static class Node {
private int iData;
public Node(int key) {iData = key;}
public int getKey() {
return iData;}
public void setKey(int id) {iData = id;}}
static class Heap {
private Node[] heapArray;
private int maxSize;
private int currentSize;
public Heap(int mx) {maxSize = mx;currentSize =
0;heapArray =
new Node[maxSize];}
public boolean isEmpty() {
return currentSize ==
0;}
public void insertAt(int index, Node node) {heapArray[index] = node;}
public void incrementSize() {currentSize++;}
public Node remove() {Node root = heapArray[
0];heapArray[
0] = heapArray[--currentSize];trickleDown(
0);
return root;}
public void trickleDown(int index) {
int largerChild;Node top = heapArray[index];
while (index < currentSize /
2) {
int leftChild =
2 * index +
1;
int rightChild = leftChild +
1;
if (rightChild < currentSize && heapArray[leftChild].getKey() < heapArray[rightChild].getKey()) {largerChild = rightChild;}
else {largerChild = leftChild;}
if (top.getKey() >= heapArray[largerChild].getKey()) {
break;}heapArray[index] = heapArray[largerChild];index = largerChild;}heapArray[index] = top;}
public void displayHeap() {
int nBlanks =
32;
int itemsPerRow =
1;
int column =
0;
int j =
0;String dots =
"-------------------------";System.
out.println(dots + dots);
while (currentSize >
0) {
if (column ==
0) {
for (
int k =
0; k < nBlanks; k++) {System.
out.print(
' ');}System.
out.print(heapArray[j].getKey());
if (++j == currentSize) {
break;}
if (++column == itemsPerRow) {nBlanks /=
2;itemsPerRow *=
2;column =
0;System.
out.println();}
else {
for (
int k =
0; k < nBlanks *
2 -
2; k++) {System.
out.print(
' ');}}}}System.
out.println(
"\n" + dots + dots);}
public void displayArray() {
for (
int j =
0; j < maxSize; j++) {System.
out.print(heapArray[j].getKey() +
" ");}System.
out.println();}}
}
?
?
?
?
?
測試用例:
package com.xingej.algorithm.sort;
import org.junit.Before;
import org.junit.Test;
public class SortTest {
private int max =
20;
private int[] arr =
new int[max];
@Beforepublic void initArray() {
for (
int i =
0; i < max; i++) {arr[i] = (
int) (Math.random() *
100 +
1);}System.out.println(
"------排序前-----");show(arr);}
private void show(int[] arr) {
for (
int i =
0; i < max; i++) {System.out.print(arr[i] +
" ");}System.out.println();}
@Testpublic void testByBubbleSort() {SortUtils.bubbleSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testBySelectionSort() {SortUtils.selectSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByInsertSort() {SortUtils.insertSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByShellSort() {SortUtils.shellSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByQuickSort() {SortUtils.quickSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByMergeSort() {SortUtils.mergeSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByBucketSort() {SortUtils.bucketSort(arr);System.out.println(
"------排序后-----");show(arr);}
@Testpublic void testByHeapSort() {SortUtils.heapSort(arr);}}
代碼已經上傳到了git上
https://github.com/xej520/xingej-algorithm
?
?
本文轉自故新51CTO博客,原文鏈接:?http://blog.51cto.com/xingej/2056881,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的JAVA 排序工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。