logo

std::sort() v C++ STL

Razpravljali smo qsort() v C. C++ STL ponuja podobno funkcijo razvrščanja, ki razvršča vektor ali matriko (elemente z naključnim dostopom)

Običajno zahteva dva parametra, prvi je točka matrike/vektorja, od koder se mora začeti razvrščanje, drugi parameter pa je dolžina, do katere želimo, da se matrika/vektor razvrsti. Tretji parameter je neobvezen in ga lahko uporabimo v primerih, če želimo elemente razvrstiti leksikografsko.



Funkcija sort() privzeto razvrsti elemente v naraščajočem vrstnem redu.

Spodaj je preprost program za prikaz delovanja sort().

CPP








// C++ program to demonstrate default behaviour of> // sort() in STL.> #include> using> namespace> std;> int> main()> {> >int> arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };> >int> n =>sizeof>(arr) />sizeof>(arr[0]);> >/*Here we take two parameters, the beginning of the> >array and the length n upto which we want the array to> >be sorted*/> >sort(arr, arr + n);> >cout <<>' Array after sorting using '> >'default sort is : '>;> >for> (>int> i = 0; i cout << arr[i] << ' '; return 0; }>

>

>

Izhod

Array after sorting using default sort is : 0 1 2 3 4 5 6 7 8 9>

Časovna zapletenost: O(N log N)
Pomožni prostor: O(1)

Kako razvrstiti v padajočem vrstnem redu?
sort() sprejme tretji parameter, ki se uporablja za določanje vrstnega reda, v katerem naj bodo elementi razvrščeni. Za razvrščanje v padajočem vrstnem redu lahko posredujemo funkcijo larger(). Ta funkcija naredi primerjavo na način, ki pred seboj postavi večje elemente.

CPP




// C++ program to demonstrate descending order sort using> // greater().> #include> using> namespace> std;> int> main()> {> >int> arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };> >int> n =>sizeof>(arr) />sizeof>(arr[0]);> >sort(arr, arr + n, greater<>int>>());> >cout <<>'Array after sorting : '>;> >for> (>int> i = 0; i cout << arr[i] << ' '; return 0; }>

>

>

Izhod

Array after sorting : 9 8 7 6 5 4 3 2 1 0>

Časovna zapletenost: O(N log N)
Pomožni prostor: O(1)

Razvrsti matriko samo v danem obsegu: Za reševanje tovrstnih težav moramo le omeniti obseg znotraj funkcije razvrščanja.
Spodaj je izvedba zgornjega primera:

C++




// C++ program to demonstrate sort()> #include> using> namespace> std;> int> main()> {> >int> arr[] = { 0, 1, 5, 8, 9, 6, 7, 3, 4, 2 };> >int> n =>sizeof>(arr) />sizeof>(arr[0]);> >// Sort the elements which lies in the range of 2 to> >// (n-1)> >sort(arr + 2, arr + n);> >cout <<>'Array after sorting : '>;> >for> (>int> i = 0; i cout << arr[i] << ' '; return 0; } // This code is contributed by Suruchi Kumari>

>

>

Izhod

Array after sorting : 0 1 2 3 4 5 6 7 8 9>

Časovna kompleksnost: O(N log N)

Pomožni prostor: O(1)

Kako razvrstiti v a posebno naročilo?
Lahko tudi napišemo lastno primerjalno funkcijo in jo posredujemo kot tretji parameter. Ta primerjalna funkcija vrne vrednost; pretvorljiv v bool, ki nam v bistvu pove, ali naj bo posredovani prvi argument postavljen pred posredovani drugi argument ali ne.
Na primer: v spodnji kodi predpostavimo, da sta intervala {6,8} in {1,9} posredovana kot argumenta v funkciji compareInterval (funkcija primerjalnika). Zdaj kot i1.first (=6)

CPP




// A C++ program to demonstrate> // STL sort() using> // our own comparator> #include> using> namespace> std;> // An interval has a start> // time and end time> struct> Interval {> >int> start, end;> };> // Compares two intervals> // according to starting times.> bool> compareInterval(Interval i1, Interval i2)> {> >return> (i1.start } int main() { Interval arr[] = { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } }; int n = sizeof(arr) / sizeof(arr[0]); // sort the intervals in increasing order of // start time sort(arr, arr + n, compareInterval); cout << 'Intervals sorted by start time : '; for (int i = 0; i cout << '[' << arr[i].start << ',' << arr[i].end << '] '; return 0; }>

>

>

Izhod

Intervals sorted by start time : [1,9] [2,4] [4,7] [6,8]>

Časovna zapletenost std::sort() je:

posodabljanje jave
  1. Najboljši primer – O(N log N)
  2. Povprečni primer – O(N log N)
  3. Najslabši primer – O(N log N)

Kompleksnost prostora: Uporablja lahko O(log N) pomožnega prostora.

C++




#include> #include> using> namespace> std;> template> <>class> T>> class> Comparator {>// we pass an object of this class as> >// third arg to sort function...> public>:> >bool> operator()(T x1, T x2)> >{> >return> x1 } }; template bool funComparator(T x1, T x2) { // vrnjena vrsta je bool return x1<= x2; } void show(int a[], int array_size) { for (int i = 0; i cout << a[i] << ' '; } } int main() { int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; int asize = sizeof(a) / sizeof(int); cout << 'The array before sorting is : '; show(a, asize); cout << endl << 'The array after sorting is(asc) :'; sort(a, a + asize); show(a, asize); cout << endl << 'The array after sorting is(desc) :'; sort(a, a + asize, greater ()); pokaži (a, velikost); cout<< endl << 'The array after sorting is(asc but our ' 'comparator class) :'; sort(a, a + asize, Comparator ()); pokaži (a, velikost); cout<< endl << 'The array after sorting is(asc but our ' 'comparator function) :'; sort(a, a + asize, funComparator ); pokaži (a, velikost); vrni 0; }>

>

>

Izhod

The array before sorting is : 1 5 8 9 6 7 3 4 2 0 The array after sorting is(asc) :0 1 2 3 4 5 6 7 8 9 The array after sorting is(desc) :9 8 7 6 5 4 3 2 1 0 The array after sorting is(asc but our comparator class) :0 1 2 3 4 5 6 7 8 9 The array after sorting is(asc but our comparator function) :0 1 2 3 4 5 6 7 8 9>

Časovna zapletenost: O(N log N)
Pomožni prostor: O(1)