Data structures and algorithms-fast sequencing (Java implementation)

Source: Internet
Author: User
Tags benchmark bitwise

[TOC]

Quick Sort Program Code
Package Com.uplooking.bigdata.datastructure;import Java.util.arrays;public class QuickSort {public static void main (St        Ring[] args) {int[] arr = {8,-2, 3, 9, 0, 1, 7, 6};        System.out.println ("Before sorting:" + arrays.tostring (arr));        QuickSort (arr);    System.out.println ("After sorting:" + arrays.tostring (arr));    } public static void QuickSort (int[] arr) {quickSort (arr, 0, arr.length-1);     /** * Quick Sort * is a divide-and-conquer idea, with a benchmark element as the standard, placing the collection in the left side of the set that is smaller than the Datum element, and vice versa.     * So we can find where the datum element is located in the collection.     * In the same way, we can find every element in the right place in the collection.     * Somewhat similar to binary lookup.     * Generally this is the first element of the base element. * Set two pointers, one setting at the beginning, one setting at the end, searching from the end, finding a smaller than the basic element to stop, and then * starting from the left to find an element that is larger than the base element, stop, the two elements are exchanged until two pointers meet, the end of the cycle * pointer point to the The position is where the datum element should be located in the collection * eg * {8,-2, 3, 9, 0, 1, 7, 6} * Benchmark * First bm=8 * end = Lengt      h-1 = 7 * start=0 * End--, We found 6:8 small, end pointer stopped, current index is j=7 * start++, until element 9 stops, current index i=3 *    Exchange the elements corresponding to I and J *{8,-2, 3, [6], 0, 1, 7, [9]} * I J * Loop continues * End--, to the position where Index 6 is stopped * start+                       + until we meet and end up with no more than 8 of the elements, so we can conclude that this position should be 8 in the set should be in the position of * Exchange 8 Index and meet index * {8,-2, 3, 6, 0, 1, 7, 9} *     I=j=6 * Exchange: {7,-2, 3, 6, 0, 1, 8, 9} * Similarly, we can repeat the above operation on the left side of 8, the right side of 8 can also repeat the above operation * using recursive call to complete the sorting of the collection            * @param arr */public static void QuickSort (int[] arr, int. Low, int.) {if (Low > High) {        Return        }//By default [Arr[low] in [Low, high] as the base value int index = Arr[low];        Defines the left pointer int start = low;        Defines the right pointer int end = high; Starts scanning to the base value, when the start < end condition is not met//indicates that the pointer is to meet, you need to exchange the base value with start/that is, the base value in the entire element collection position has been determined//its left value is smaller than the base value, its  The value on the right is larger than the base value while (Start < end) {//According to the design of the previous algorithm, start scanning from the right until the number smaller than the base value is found and then stop//(the meaning of the loop below is that the start < end, if the value of the end position is greater or equal than the index value, continue to the left) while (Start < end && Arr[enD] >= index) {end--;            }//Wealth starts scanning from the left until it finds a number that is larger than the base value and then stops//(the meaning of the loop below is that, with start < end, if the value of the start position is smaller or equal than the index value, continue to the right)            while (Start < end && Arr[start] <= index) {start++;                } if (Start < end) {//Before the end of the loop, if start is less than end//then swap Arr[start] and Arr[end] values            Swap (arr, start, end);        }}//At the end of the loop above, start pointer and end pointer meet, Exchange Arr[start] and datum value arr[low] = Arr[start];        Arr[start] = index;        After the exchange, Arr[start] left is smaller than it, the right is larger than it, and then it is the same as the base//left and right quickSort (arr, Low, start-1);    QuickSort (arr, start + 1, high); }/** * Bitwise operation * Bitwise AND (&) * 1&1=1 * 1&0=0 * 0&1=0 * 0&0=0 * XOR (^) * (Values differ  ) Take True (1), otherwise (0) * 1&1=0 * 1&0=1 * 0&1=1 * 0&0=0 * Non * * @param arr * @param I * @param j */Private Static void Swap (int[] arr, int i, int j) {/*int tmp = arr[i];        Arr[i] = Arr[j];        ARR[J] = tmp;*/Arr[i] = arr[i] ^ arr[j];        ARR[J] = Arr[i] ^ arr[j];    Arr[i] = Arr[i] ^ arr[j];     }}/* Exchange A=3 and b=5 without third-party variables, the most efficient method one: a = a + b = 8 B = A-c = (8-5) = 3 A = b = (8-3) = 5 xor mode         a=3--> low 8 for 0000 0011 b=5--> Low 8 for 0000 0101 A = a ^ b 0000 0011 ^ 0000 0101---------------     0000 0110--->6 b = a ^ b 0000 0110 ^ 0000 0101--------------0000 0011--->3 A = a ^ b 0000 0110 ^ 0000 0011---------------0000 0101--->5*/
Test

The results of the implementation are as follows:

排序前:[8, -2, 3, 9, 0, 1, 7, 6]排序后:[-2, 0, 1, 3, 6, 7, 8, 9]

Data structures and algorithms-fast sequencing (Java implementation)

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.