8. Bubble Sorting and quick sort (based on OPENCV)

Source: Internet
Author: User

First, preface

This paper mainly describes the basic process of bubble sorting and quick sorting, and gives the code implementation, and the pro-test is available.

Second, bubble method sort

The bubble method is mainly to compare the adjacent two values, the small forward bubble, large back precipitation, time complexity of O (N2). The main ideas are as follows:

divided into internal and external loops, each outer loop to determine the specific location of a large data, the following example:

As can be seen, in two times the outer loop can be sorted results, the subsequent 8 cycles are wasted, in order to avoid this situation, we can set a state parameter, used to indicate whether the inner loop is the exchange of data, so as to whether the outer loop exits the signal.

Three, quick sort

Fast sorting is one of the most effective sorting methods, the main idea is to sort the data separately, by selecting a cardinality, then dividing the data to be sorted into two parts, all large and small on one side. Then the recursive idea is used to sort the separate data.

Iv. implementation of the Code

Bubble sort (the position of the output sorted data in the original data):

1 voidSparsegraphic::bubblesort (ConstCv::mat Inmat, qvector<int> &index, Cv::mat &Outmat)2 {3     if(inmat.rows!=1)4         return;5     intCol =Inmat.cols;6index = qvector<int>(col);7      for(inti =0; i<col;i++)8     {9Index[i] =i;Ten     } One     if(Inmat.type ()! =cv_32f) A Inmat.convertto (inmat,cv_32f); -Outmat =Inmat.clone (); -  the     float*ptr = outmat.ptr<float> (0); -     floatTmpval; -     intTmpidx; -      for(inti =0; i<col;i++) +     { -          for(intj =0; J<col-i-1; j + +) +         { A             if(ptr[j]>ptr[j+1]) at             { -Tmpval =Ptr[j]; -ptr[j]=ptr[j+1]; -ptr[j+1]=Tmpval; -Tmpidx =Index[j]; -INDEX[J] = index[j+1]; inindex[j+1] =Tmpidx; -             } to         } +     } -}

Quick sort (outputs the position of the data in the source data after sorting):

1 voidSparsegraphic::quicksort (Cv::mat Inmat, qvector<int> &index,intLowintHigh )2 {3     if(inmat.rows!=1)4         return;5     float*ptr = inmat.ptr<float> (0);6     if(Low <High )7     {8         inti = Low,j =High ;9         floatX =Ptr[low];Ten          while(i<j) One         { A              while(I<j && Ptr[j] >=X) -j--; -             if(i<j) the             { -ptr[i++] =Ptr[j]; -                 intTMP =Index[j]; -INDEX[J] = index[i-1]; +index[i-1] =tmp; -             } +  A              while(I<j && Ptr[i] <X) ati++; -             if(i<j) -             { -ptr[j--] =Ptr[i]; -                 intTMP =Index[i]; -Index[i] = index[j+1]; inindex[j+1] =tmp; -             } to         } +Ptr[i] =X; -QuickSort (inmat,index,low,i-1); theQuickSort (inmat,index,i+1, high); *     } $}

8. Bubble Sorting and quick sort (based on OPENCV)

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.