Qsort test examples for each data type

Source: Internet
Author: User

It's very rewarding.

Qsort demo#include <iostream> #include <cstdlib>//qsort #include <cstring>using namespace Std;int Cmp_int (const void *a,const void *b) {return * (int *) A-* (int *) b;} int Cmp_char (const void *a,const void *b) {return * (char *) A-* (char *) b;} int cmp_string (const void *a,const void *b) {return strcmp ((char*) A, (char *) b);} struct Node {int x, y;}; int Cmp_struct1 (const void *a,const void *b)//The struct-level sort (sorted by a keyword in the struct) {return (* (node*) a). x > (* (node*) b). x 1:-1;} int cmp_struct2 (const void *a,const void *b)//Structure two-level sort (by X from small to large, {//when x equals y from large to small compared) struct Node *c = (Node *) a;struct Nod E *d = (Node *) b;if (c->x!=d->x) return C->x-d->x;elsereturn c->y-d->y;} int main () {//Test qsort int Arr[6] = {5,3,12,8,1,9};qsort (arr,6,sizeof (int), cmp_int); for (int i=0;i<6;i++) {cout <<arr[i]<< "";} cout<<endl;//Test Qsort Char is ordered char ch[6] = {' V ', ' a ', ' w ', ' K ', ' Z ', ' C '};qsort (ch,6,sizeof (char), Cmp_char); for ( int i=0;i<6;i++) {cout<<ch[i]<< "";} cout<<endl;//Test Qsort The string sort char str[100][10]= {"abc", "CDF", "ckg", "MVC", "Java", "Oracle"};qsort (str,6,sizeof ( Str[0]), cmp_string); for (int i=0;i<6;i++) {cout<<str[i]<< "";} cout<<endl;  Test Qsort A first-level sort of struct (in order of size in x fields) struct Node node[100] ={{12,15},{30,25}, {20,8},{28,10},{35,5}, {30,10}}; Qsort (Node,6,sizeof (node[0]), cmp_struct1);   for (int i=0;i<6;i++) {cout<<node[i].x<< "" <<node[i].y<< "\ T";} cout<<endl;  Test Qsort Two-level sort of struct Node node2[100] ={{12,15},{30,25},{20,16},{20,10},{35,5}, {30,10},{32,6},{35,50}}; Qsort (Node2,8,sizeof (node2[0]), cmp_struct2); for (int i=0;i<8;i++) {cout<<node2[i].x<< "" <<node2[i].y<< "\ T";} cout<<endl; return 0;}


Qsort test examples for each data type

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.