A fast sort Java implementation of sorting algorithm

Source: Internet
Author: User

A quick sort of sorting algorithm

Dance Demo Sort:

Bubble Sort: Http://t.cn/hrf58M

Hill sort: HTTP://T.CN/HROSVB

Select Sort: http://t.cn/hros6e

Insert Sort: Http://t.cn/hros0W

Quick Sort: http://t.cn/ScTA1d

Merge Sort: Http://t.cn/Sc1cGZ

Quick Sort is an in-place sort, divide-and-conquer, large-scale recursive algorithm. essentially, It is the in-place version of the merge Sort.

1. Quick sort can be made up of the following four steps:
(1)if not more than1Data and return directly.
( 2) generally select the leftmost value of the sequence as the pivot Data.
( 3) divide the sequence into 2 parts, part of which is larger than the pivot data, and the other part is smaller than the pivot Data.
( 4) use recursion to sort the sequences on both Sides.

fast sorting is faster than most sorting algorithms. Although we can write algorithms that are faster than fast sorting in some special cases, there is usually no faster than that. Fast sorting is recursive, and it is not a good choice for machines with very limited memory.
2. Fast Sorting code Implementation

Package cn.com.zfc.lesson21.sort;

/**
*
* @title QuickSort
* @describe Quick Sort
* @author Zhang Fuchang
* @date October 2, 2016 2:45:37
*/
public class QuickSort {
Fast sorting is the fastest sorting algorithm in the average Time.
Basic Idea: One of the data elements in the selected sequence (usually the first Data Element) as a pivot, which is compared with all the remaining elements, and all the smaller elements are in front of it;
Put all the larger elements behind it, after a trip to sort, the data element can be in the same position as the boundary, the serializable can be divided into two parts;
Repeat the process for each of the two sections until only one data element is left in each of the Sections.

public static void main (string[] Args) {
Declaring an integral type array
int[] array = new int[10];
Initializing an array with loops and random numbers
for (int i = 0; i < array.length; i++) {
array[i] = (int) math.round (math.random () * 100);
}
System.out.println ("original Array is:");
For (int I:array) {
System.out.print (i + "");
}
System.out.println ();
System.out.println ("sorted Array is:");
For (int I:quicksort (array)) {
System.out.print (i + "");
}
}

/**
*
* Function: quickly sort the array and return it
*
* Parameter: int[] arr
*
* Return type: int[]
*/
public static int[] QuickSort (int[] Arr) {
Quicksorthelp (arr, 0, arr.length-1);
Return arr;
}

/**
*
* Functions: Quick sorting of records in array arr[low...high]
*
* parameters: int[] arr, int low, int high
*
* Return type: void
*/
public static void Quicksorthelp (int[] arr, int. low, Int.) {
If (low < High) {
Sub-sequence elem[low...high] is longer than 1
int pivotloc = partition (arr, low, high);
Sub-sequence arr[low...pivotloc-1] Recursive ordering
Quicksorthelp (arr, low, pivotLoc-1);
Sub-sequence arr[pivotloc+1...high] Recursive ordering
Quicksorthelp (arr, Pivotloc + 1, high);
}
}

/**
*
* function: to move the pivot element to the correct position, the element to the left of the pivot is not greater than the pivot, the element to the right of the pivot is not less than the pivot, and the pivot position is returned
*
* parameter: int[] arr, int low,int hi GH
*
* return type: int
*/
public static int partition (int[] arr, int. low, int.) {
while (low &L T High) {
///arr[low] is pivot so that the element to the right of the high is not less than elem[low]
while (low < high && arr[high] >= Arr[lo      W]) {
high--;
      }
//interchange arr[low] and arr[high] of the value
Swap (arr, low, high);
        Arr[high] is pivot so that the element on the left side of low is not more than arr[high]
while (low < high && arr[low] <= arr[high]) {
low++;    
}
//interchange arr[low] and arr[high] values
Swap (arr, low, high);
  
//return to pivot position
return low;
}

/**
*
* Function: Exchange two number of values
*
* Parameters: int X,int y
*
* Return type: void
*/
public static void swap (int[] arr, int i, int J) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

A fast sort Java implementation of 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.