Two-dimensional array quick sort (sort+qsort)

Source: Internet
Author: User

Fast ordering of two-dimensional arrays

Qsort is a quick sort in C, and if a simple one-dimensional array is sorted, you know it. Now look at the ordering of the two-dimensional array, although it can be bubbling but too time-consuming, we use qsort here to quickly sort, see the code should be able to read it.

Code:

1#include <stdio.h>2#include <stdlib.h>3 structnode4 {5     intx, y;6}a[10005];7 intcmpConst void*a,Const void*B)//to convert to struct type8 {9     structNode *c = (node*) A;Ten     structNode *d = (node*) b; One     returnC->y-d->y; A } - intMain () - { the     intI,m,n; -scanf"%d",&m); -      while(m--) -     { +scanf"%d",&n); -          for(i=0; i<n; i++) +scanf"%d%d",&a[i].x,&a[i].y); AQsort (A,n,sizeof(a[0]), CMP);//remember is sizeof (A[0])

Sort is a quick sort in C + +, not much, directly on the code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 structnode6 {7     intx, y;8}a[10005];9 BOOLCMP (node A,node b)Ten { One     returnA.y <b.y; A } - intMain () - { the     intM,n,i,k,ans; -scanf"%d",&m); -      while(m--) -     { +scanf"%d",&n); -          for(i=0; i<n; i++) +scanf"%d%d",&a[i].x,&a[i].y); ASort (a,a+n,cmp);

Two-dimensional array quick sort (sort+qsort)

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.