Java implementation of data structure sorting algorithm

Source: Internet
Author: User

Idle boredom and picked up the University of the data structure of the book looks "data structure (c language version)" Min 聯繫 authored.

There are several sorting algorithms, feel very fun, just want to write out to play.

Package test.sort;/** * @Title: Todo * @Description: TODO * @author: * @date: 2014-8-10 a.m. 11:20:43 * */public CLA SS QuickSort {private static int datas[] = {23,42,12,45,56,63,24,41,32};/** * @Title: Main * @Description: TODO * @par Am @param args * @return void return type * @author * @time 2014-8-10 Morning 11:20:43 * @throws */public static void Main (S Tring[] args) {//bubblesort ();//quicksort (datas, 0, datas.length-1);//selectsort (datas); Heapsort (datas); for (int Data:datas) {System.out.print (data + "");}} Bubble sort public static void Bubblesort () {for (int i = 0; i < datas.length-1; i++) {for (int j = i+1; J < Datas.length; j + +) {int num = 0;if (Datas[i] > Datas[j]) {num = Datas[i];d atas[i] = datas[j];d atas[j] = num;}}}  public static int partition (int datas[], int. Low, Int. high) {int pivotkey = Datas[low];while (Low < High) {while (low < high && Datas[high] >= pivotkey) high--;d Atas[low] = datas[high];while (Low < high && Datas[lo W] <= PIvotkey) {low++;} Datas[high] = Datas[low];} Datas[low] = Pivotkey;return low;}  Quick Sort algorithm public static void QuickSort (int datas[], int low, int high) {if (Low < high) {int pivotloc = partition (Datas, QuickSort (datas, Low, pivotloc-1); QuickSort (Datas, pivotloc+1, High);}} Select sort public static void Selectsort (int datas[]) {for (int i = 0; i < datas.length-1; i++) {int J = Selectminkey (Datas, i); if (i! = j) {int num = datas[i];d atas[i] = datas[j];d atas[j] = num;}}
Find out where the smallest value in the array is located public static int Selectminkey (int datas[],int i) {int minindex = I;int Mindata = datas[i];for (int j = i+ 1; J < Datas.length; J + +) {if (Mindata > Datas[j]) {minindex = J;mindata = Datas[j];}} return minindex;} A heap is formed from the first S element for the root node, that is, to filter public static void Heapadjust (int datas[], int s, int m) {int rc = datas[s];for (int i = 2*s+1; I & Lt M i*=2) {if (I < m-1 && Datas[i] < datas[i+1]) i++;if (RC >= datas[i]) {break;} Datas[s] = Datas[i];s = i;} Datas[s] = RC;} Heap sort public static void Heapsort (Int. datas[]) {for (int i = datas.length/2-1; I >= 0; i--) heapadjust (datas, I, DATAS.L Ength); for (int j = datas.length-1; j > 0; j--) {int num = datas[0];d atas[0] = datas[j];d atas[j] = num;heapadjust (data S, 0, J);}}}

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.