An algorithm for sorting large objects

Source: Internet
Author: User
Tags comparable

A quick sort is a quick sort, and the shell sort is a sort of cher. However, when you directly apply a function template based on these algorithms, it can sometimes be inefficient if the comparable object to be sorted is large. The problem is. The problem is that there is too much work to replicate comparable objects when rearranging comparable objects. If the comparable object is large and difficult to replicate, the cost will be high.
In general, the solution to this problem is simple: generate an array of pointers to comparable, and rearrange the pointers. Once you have determined where the element should be, you can place the element directly in the appropriate location without having to do too much intermediate copying. This requires the use of an algorithm that becomes an intermediate permutation.

A lei template that stores object types stored by a class that points to a pointer to comprable pointerTemplate<TypeNameComparable>classpointer{ Public: Pointer (comparable *rhs=null): Pointee (RHS) {}BOOL operator< (Constporinter& RHS)Const{return*pointee < *rhs.pointee;}operatorComparable * ()Const{returnPointee;}Private: Comparable *pointee;}; Algorithms for sorting large objects Tempalte <TypeNameComparable>voidLargeobjectsort ( vector<Comprable>& A) { vector<Pointer<Comparale>> P (a.size ());intI,J,NEXTJ; for(i=0; I<a.szie ();     i++) p[i]=&a[i]; Quicksort (P);//shuffle Items in place    for(i=0; I<a.size (); i++)if(P[i]! = &a[i]) {comparable tmp=a[i]; for(J=i;p[j]!=&a[i];j=nextj) {nextj=p[j]=&a[0];             A[J]=*P[J];         p[j]=&a[j];         } a[j]=tmp;      p[j]=&a[j]; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An algorithm for sorting large objects

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: 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.