Java Sorting algorithm

Source: Internet
Author: User

Overview:

Here is a demonstration of the various sorting algorithms using Java.


Bubble Sort

public class Bubblesort {public static int[] Bubblesort (int[] array) {if (array = = NULL) {return null;} for (int i = 0, i < array.length; i++) {for (int j = i + 1; j < Array.Length; J + +) {if (Array[i] > Array[j]) {arr Ay[i] = Array[i] + array[j];array[j] = Array[i]-array[j];array[i] = Array[i]-array[j];}}} return array;}}

Insert Sort

public class Insertsort {public static int[] Insertsort (int[] array) {if (array = = NULL) {return null;} for (int i = 1; i < Array.Length; i++) {for (int j = i; (J > 0) && (Array[j] < array[j-1]); j--) {Sortutils.swap (array, J, j-1);}} return array;}}

Select Sort

public class Selectionsort {public static int[] Selectionsort (int[] array) {if (array = = NULL) {return null;} for (int i = 0; i < Array.Length; i++) {int lowindex = i;for (int j = array.length-1; j > i; j--) {if (Array[j] &l T Array[lowindex]) {lowindex = j;}} Sortutils.swap (Array, I, Lowindex);} return array;}}

Shell Sort

public class Shellsort {public static int[] Shellsort (int[] array) {if (array = = NULL) {return null;} for (int i = ARRAY.LENGTH/2; i > 2; I/= 2) {for (int j = 0; J < i; J + +) {Insertsort (array, J, i);}} Insertsort (Array, 0, 1); return array;} private static void Insertsort (int[] array, int start, int Inc) {for (int i = start + inc; I < array.length; i + = Inc) {for (int j = i; (J >= Inc) && (Array[j] < array[j-inc]); J-= Inc) {Sortutils.swap (array, J, J-inc);}}}

Quick Sort

public class Qksort {public static int[] QuickSort (int[] array) {if (array! = NULL) {return QuickSort (array, 0, array.length-1);} return null;} private static int[] QuickSort (int[] array, int beg, int end) {if (Beg >= end | | array = = NULL) {return null;} int p = partition (array, beg, end), QuickSort (array, Beg, p-1), QuickSort (array, p + 1, end); return array;} /** * Find the demarcation point * @param array * @param beg * @param end * @return */private static int partition (int[] array, int beg, int en d) {int last = Array[end];int i = beg-1;for (int j = Beg; J <= End-1; + j) {if (Array[j] <= last) {i++;if (i! = j) {Array[i] = Array[i] ^ array[j];array[j] = Array[i] ^ Array[j];array[i] = Array[i] ^ array[j];}} if ((i + 1) = end) {array[i + 1] = array[i + 1] ^ array[end];array[end] = array[i + 1] ^ Array[end];array[i + 1] = array[ i + 1] ^ array[end];} return i + 1;}} 

Heap Sort

public class Heapsort {public static int[] Heapsort (int[] array) {if (array = = NULL) {return null;} Maxheap h = new Maxheap (), H.init (array), for (int i = 0; i < Array.Length; i++) {H.remove ();} System.arraycopy (H.queue, 1, array, 0, array.length); return array;} private static class Maxheap {void init (int[] data) {This.queue = new Int[data.length + 1];for (int i = 0; i < Data.len Gth i++) {Queue[++size] = data[i];fixup (size);}} private int size = 0;private int[] queue;public int get () {return queue[1];} public void Remove () {Sortutils.swap (queue, 1, size--); Fixdown (1);} fixdownprivate void Fixdown (int k) {int J;while ((j = k << 1) <= size) {if (J < size && Queue[j] &l T Queue[j + 1]) {j + +;} Do not swap if (Queue[k] > Queue[j]) {break;} Sortutils.swap (Queue, J, k); k = j;}} private void fixup (int k) {while (K > 1) {int j = k >> 1;if (Queue[j] > Queue[k]) {break;} Sortutils.swap (Queue, J, k); k = j;}}}

Merge Sort

public class MergeSort {public static int[] MergeSort (int[] array) {if (array = = NULL) {return null;} int[] temp = new Int[array.length];return mergesort (array, temp, 0, array.length-1);} private static int[] MergeSort (int[] array, int[] temp, int l, int r) {int mid = (L + R)/2;if (L = = r) {return null;} MergeSort (Array, temp, L, mid), MergeSort (Array, temp, mid + 1, R), for (int i = l; I <= R; i++) {temp[i] = array[i];} int i1 = L;int I2 = mid + 1;for (int cur = l; cur <= r; cur++) {if (I1 = = Mid + 1) {Array[cur] = temp[i2++];} else if ( I2 > R) {array[cur] = temp[i1++];} else if (Temp[i1] < Temp[i2]) {array[cur] = temp[i1++];} else {array[cur] = temp[ i2++];}} return array;}}

merge sort (improved)

public class Mergesortimproved {private static final int THRESHOLD = 10;public static int[] MergeSort (int[] array) {if (AR Ray = = null) {return null;} int[] temp = new Int[array.length];return mergesort (array, temp, 0, array.length-1);} private static int[] MergeSort (int[] array, int[] temp, int l, int r) {int I, j, k;int mid = (L + R)/2;if (L = = r) {Retu RN null;} if ((mid-l) >= THRESHOLD) {mergesort (array, temp, L, mid),} else {Insertsort (array, L, mid-l + 1);} if ((R-mid) > THRESHOLD) {mergesort (array, temp, mid + 1, r),} else {Insertsort (array, mid + 1, r-mid);} for (i = l; I <= mid; i++) {temp[i] = array[i];} for (j = 1; J <= R-mid; j + +) {temp[r-j + 1] = Array[j + mid];} int a = Temp[l];int B = temp[r];for (i = l, j = r, k = l; k <= R; k++) {if (a < b) {Array[k] = Temp[i++];a = Temp[i] ;} else {Array[k] = Temp[j--];b = Temp[j];}} return array;} private static void Insertsort (int[] array, int start, int len) {for (int i = start + 1; i < start + len; i++) {for (int j = i; (J > Start) && array[j] < array[j-1]; j--) {Sortutils.swap (array, J, j-1);}}}

full Source code download (includes test case and Tool Class):

http://download.csdn.net/detail/u013761665/8698287

Java Sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.