The quick ordering of the second chapter of the sword offer-

Source: Internet
Author: User

Algorithms: Sorting and finding (binary lookup, merge sort, quick sort), bit arithmetic, etc.

Find: Order lookup, hash lookup, binary sort tree lookup, hash table.

Two-point lookup resolves issues such as "the smallest number in a rotated array", "number of occurrences in a sorted array".

Hash table: Advantages: O (1) The most efficient to find an element in time.

Cons: Additional space is required to implement the hash table.

Binary sorting Tree lookup: Corresponding two search tree lookup, you can solve: "Binary tree Follow-up traversal", "two search tree and two-way linked list"

Sorting: Sorting algorithm to be skilled, additional space consumption, average time complexity and the worst time complexity comparison algorithm direct advantages and disadvantages.

Quick sort: The overall average efficiency is the best, but not always, for the ordered array, each round of sorting should be the last one to do the standard. Time Complexity of O (N2)

Idea: The key is to select a number a (random selection) in the array, move the number less than a in the array to the left of a, the number greater than a is moved to the right of a.

The implementation function is int partition (int a[],int length,int Start,int end) {if length is less than or equal to 0 or start is less than or equal to 0 or end is greater than length or a is empty. Throw an empty exception. with int randominrange (int start,int end) {if End>start{srand (time (NULL)), return Start+rand ()% (End-start)}} randomly generates the function, Generate index. Swap the value at index and end with void swap_element (int *a,int*b). Defines an shaping variable small (pointing to the pivot value that ultimately holds the initial index point) and initializes it. Initialize index and assign value to start. When index satisfies Index<end, if (A[index]<a[end]) {++small;if (index! =small) Exchange index and small values. and will index++. }。 ++small, the number that is smaller than the pivot value is placed in front of the small pointer at this point, exchanging values at end and small. }

The partition function can be used to resolve: The long team finds the number of K or the smallest number of k in the N array, or the number of occurrences more than half the number in the array.

The function that implements the fast row is void QuickSort (int a[],int length,int Start,int end) {if start==end. Returns NULL. The position of the pivot Value index after the first trip is determined by the partition function. if (Index>start) recursively calls Quicksort to the left sort. The IF (index<end) recursive call quicksort to the right of the sort. }

#include <iostream>using namespace std; #include <time.h>int randominrange (int start, int end) {if (end> Start) {Srand (Time (NULL));//Srand function is the initialization function of the random number generator, which causes the random number seed to change over time to return Start+rand ()% ((End-start));//Generate start~ The random number between end}return 0;} void swap_element (int *a,int *b) {int temp;temp=*a;*a=*b;*b=temp;} int Partition (int data[],int length,int Start,int end) {if (Data==null | | length<=0| | start<0 | | end>=length) {cout<< "error1!" <<endl;exit (0);}    int Index=randominrange (start,end); Swap_element (&data[index],&data[end]);    int small=start-1;    for (index=start;index<end;index++) {if (Data[index]<data[end]) {++small; if (small! = index) {swap_element (&data[index],&data[small]);}}}    ++small; Swap_element (&data[small],&data[end]); return small;} void Quicksort (int data[],int length,int Start,int end) {if (start==end) {return;} int index=partition (data,length,start,end); if (Index>start) Quicksort (data,length,start,index-1); if(Index<end) Quicksort (data,length,index+1,end);} int main (void) {int data[10]={1,4,7,0,6,10,3,8,2,9}; Quicksort (data,10,0,9); for (int i=0;i<10;i++) cout<<data[i]<< ""; Cout<<endl;return 0;}

The quick ordering of the second chapter of the sword offer-

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.