This week, we studied the optimization strategy for fast sequencing, first of all using randomization to optimize for fast sequencing.
As we all know, before the basic fast sorting algorithm, its efficiency is a key point in the selection of elements, because the previous selection is the first element, so when encountering a special input, such as too large or too small, it will cause the interval division is extremely unreasonable.
The introduction of randomization, is to randomly select an element as a keyword at each partition, and use it to divide. Since each division is randomly selected, each time a bad element is chosen, the probability is low and can be used as an optimization direction.
And before the basic quick sort the main difference there are two
1. First, the partition section
Using Rand to generate random numbers, where flag is the subscript of the keyword, is generated by random; PivotKey is the value of the array element that should be subscript, note that the swap portion is exchanged for flag and high or low, rather than the high and low swaps of the previous base fast, and the second is to note that the random number is generated in the range of low and high and can be set by the offset (+ Low) to achieve
1 intPartitionintArr[],intLowintHigh )2 {3 4 intpivotkey, Flag;5Flag = (rand ()% (high-low)) +Low ;6PivotKey =Arr[flag];7 while(Low <High )8 {9 while(Low < High && (Arr[high] >=PivotKey))Ten { Onehigh--; A } - if(Low <High ) - { the swap (ARR, flag, high); - } - while(Low < High && (Arr[low] <=PivotKey)) - { +low++; - } + if(Low <High ) A { at swap (ARR, flag, low); - } - } - returnLow ; -}
2.swap part, the switching element has changed
1 voidSwapintArr[],int&flag,intN)2 {3 inttemp;4 //array element Exchange First5temp =Arr[flag];6Arr[flag] =Arr[n];7Arr[n] =temp;8 //re-subscript exchange9 TenFlag =N; One}
The complete code reference is released below:
1 //fast sequencing-randomization optimization2 //3 4#include"stdafx.h"5#include <iostream>6#include <list>7#include <ctime>8 9 using namespacestd;Ten One voidSwapintArr[],int&flag,intN) A { - inttemp; - //array element Exchange First thetemp =Arr[flag]; -Arr[flag] =Arr[n]; -Arr[n] =temp; - //re-subscript exchange + -Flag =N; + } A at intPartitionintArr[],intLowintHigh ) - { - - intpivotkey, Flag; -Flag = (rand ()% (high-low)) +Low ; -PivotKey =Arr[flag]; in while(Low <High ) - { to while(Low < High && (Arr[high] >=PivotKey)) + { -high--; the } * if(Low <High ) $ {Panax Notoginseng swap (ARR, flag, high); - } the while(Low < High && (Arr[low] <=PivotKey)) + { Alow++; the } + if(Low <High ) - { $ swap (ARR, flag, low); $ } - } - returnLow ; the } - Wuyi voidQsortintArr[],intLowintHigh ) the { - intpivot; Wu if(Low <High ) - { AboutPivot =partition (arr, low, high); $Qsort (arr, low, pivot-1); -Qsort (arr, pivot+1, high); - } - } A + int_tmain (intARGC, _tchar*argv[]) the { - inta[6] = { One,9,5,2,2,0}; $ theQsort (A,0,5); the the for(inti =0; I <6; i++) the { -cout << A[i] <<Endl; in } the the return 0; About}
Optimization of fast sequencing (i) randomization fast sequencing