Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Select sort and insert sort (thirty)

Shulou Source: shulou.com Published: 2022-06-03 04:22:55 09月29日 Update

Today let's take a look at sorting, so what is sorting? Sorting is an operation that is often carried out inside the computer, and its purpose is to adjust a set of "unordered" data elements to "ordered" data elements. So what is the mathematical definition of sorting? As follows

Let's look at a concept: the stability of ordering. What is the stability of sorting? It means that if there are two data elements r [I] and r [j] in the sequence, their keywords k [I] = = k [j], and before sorting, the object r [I] precedes r [j]; if after sorting, the object r [I] is still in front of r [j], the sorting method is said to be stable, otherwise it is said to be unstable.

Let's take a look at multi-keyword sorting. This is that there is more than one keyword to compare when sorting, so what does it mean? That is, when the sorting results are sorted first by keyword 1, by keyword 2 when keyword 1 is the same, and by keyword n when the keyword nmai 1 is the same. For multi-keyword sorting, we only need to consider multiple keywords at the same time when comparing operations. Let's analyze it through a sample code.

# include # include "Object.h" using namespace std;using namespace DTLib;struct Test: public Object {int key1; int key2; Test (int K1, int K2) {key1 = K1; key2 = k2;} bool operator = = (const Test& t) {return (key1 = = t.key1) & & (key2 = = t.key2) } bool operator! = (const Test& t) {return! (* this = = t);} bool operator > (const Test& t) {return (key1 > t.key1) | ((key1 = t.key1) & & (key2 > t.key2));} bool operator t);} bool operator = (const Test& t) {return! (* this < t);}} Int main () {Test T1 (3,4); Test T2 (3,5); Test T3 (2,4); Test T4 (1,2); cout

Tags: Sort element key keyword selection data method result facet code time example complexity minimum order location object performance stability space Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Docker OPPO Reno MySQL