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)