Quick sorting implementation

Source: Internet
Author: User

1. End condition: Low = high

2. Fast sorting needs to be separated and governed. Therefore, the location to be split is the location where the current element is arranged.

3. For a single arrangement, an element is placed in the correct position. Her left side is smaller than her, and her right side is larger than her.

package com.jue.quicksort;public class Quicksort {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint data[] = { 8, 3, 7, 1, 5, 6, 4, 9, 2 };logs("old : ", data);quickSort(data, 0, data.length - 1);logs("new : ", data);}static void logs(String str, int[] data) {StringBuffer sb = new StringBuffer(str);for (int value : data) {sb.append(value + " ");}System.out.println(sb);}public static void quickSort(int[] data, int low, int high) {if (low >= high) {return;}int index = getSortedIndex(data, low, high);quickSort(data, low, index - 1);quickSort(data, index + 1, high);}private static int getSortedIndex(int[] data, int low, int high) {int currentData = data[low];int currentDataIndex = low;while (low < high) {if (currentDataIndex == low) {if (currentData > data[high]) {data[low] = data[high];data[high] = currentData;currentDataIndex = high;low++;} else if (currentData <= data[high]) {high--;}} else if (currentDataIndex == high) {// move to leftif (currentData < data[low]) {//data[high] = data[low];data[low] = currentData;currentDataIndex = low;high--;} else if (currentData >= data[low]) {low++;}}}return currentDataIndex;}}

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.