Chinaunix

标题: sort [打印本页]

作者: shihyu    时间: 2011-01-05 13:46
标题: sort
本帖最后由 shihyu 于 2011-07-12 19:10 编辑

sss
作者: hellioncu    时间: 2011-01-05 13:59
看不懂按什么排序的
作者: liyandong106    时间: 2011-01-05 14:06
你这个排序是什么思想啊?
作者: ecjtubaowp    时间: 2011-01-05 14:15
确实看不懂。
作者: shihyu    时间: 2011-01-05 14:17
本帖最后由 shihyu 于 2011-07-12 19:14 编辑

............
作者: ecjtubaowp    时间: 2011-01-05 14:30
汗,LZ编辑一次又错了吧,应该是下面的吧:
8,10,99,0,-5,11,88,33
10,8,11,-8,0,0,77,52
10,20,-12,23,55,60,-51,19
10,20,8,10,7,-7,44,55
30,41,56,61,103,-10,0,12

作者: shihyu    时间: 2011-01-05 14:53
ecjtubaowp我改过来了

请问这样你有写过类似代码?

谢谢
作者: ecjtubaowp    时间: 2011-01-05 15:16
本帖最后由 ecjtubaowp 于 2011-01-05 15:27 编辑

回复 7# shihyu


以前写过,好像很烦琐,类似于字符串数组的排序,写一个比较函数,

  1. int c[5][8];

  2. int cmp(const void *a,const void *b)
  3. {

  4. if ((int *)a[0] != (int *)b[0])return (int *)a[0]-(int *)b[0];
  5. else if ((int *)a[1] != (int *)b[1])return (int *)a[1]-(int *)b[1];
  6. else if ((int *)a[2] != (int *)b[2])return (int *)a[2]-(int *)b[2];
  7. ...//省略
  8. }

  9. qsort(c,5,sizeof(c[0]),cmp)
复制代码
这个比较函数不知道如何化简
作者: shihyu    时间: 2011-01-06 01:14
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <iterator>
  5. #include <vector>

  6. namespace facu
  7. {
  8.    template <typename ElementType, size_t SIZE>
  9.    bool arrayLessThan( ElementType (*first)[ SIZE ],
  10.                        ElementType (*second)[ SIZE ] )
  11.    {
  12.       return std::lexicographical_compare( *first, *first + SIZE,
  13.                                            *second, *second + SIZE,
  14.                                            std::less<ElementType>() );
  15.    }
  16. }

  17. int main()
  18. {   
  19.    using namespace std;
  20.    using namespace facu;
  21.    

  22.    int arrays[ 5 ][ 8 ] = {
  23.                             { 10, 20,-12, 23, 55, 60,-51, 19 },
  24.                             { 10, 20,  8, 10,  7, -7, 44, 55 },
  25.                             { 30, 41, 56, 61,103,-10,  0, 12 },
  26.                             { 8,  10, 99,  0, -5, 11, 88, 33 },
  27.                             { 10,  8, 11, -8,  0,  0, 77, 52 }
  28.                           };
  29.                         
  30.    typedef int (*ArrayPointer)[ 8 ];
  31.    vector<ArrayPointer> pointers;

  32.    for( ArrayPointer begin = &arrays[ 0 ], end = begin + 5;
  33.         begin != end; ++begin )
  34.       pointers.push_back( begin );


  35.    sort( pointers.begin(), pointers.end(),
  36.          arrayLessThan<int,8> );


  37.    for( int row = 0; row != 5; ++row )
  38.    {
  39.       copy( *pointers[ row ], *pointers[ row ] + 8,
  40.             ostream_iterator<int>( cout, " " ) );
  41.          
  42.       cout << endl;
  43.    }
  44.    

  45.    cout << endl;
  46.    
  47.    cout << "The largest one is "
  48.         << distance( &arrays[ 0 ], pointers.back() )
  49.         << "th element." << endl;
  50.    
  51.    cout << "The smallest one is "
  52.         << distance( &arrays[ 0 ], pointers.front() )
  53.         << "th element." << endl;
  54.         
  55. }// end of function 'main'
复制代码
上面是别人用C++写的 , 但我想要用纯C写 , 不知道有人写过吗?

谢谢
作者: bruceteen    时间: 2011-01-06 08:39
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int __cdecl compare( const void* a, const void* b )
  4. {
  5.         const int* pa = (const int*)a;
  6.         const int* pb = (const int*)b;

  7.         for( unsigned int i=0; i<8; ++i )
  8.         {
  9.                 if(pa[i]<pb[i]) return -1;
  10.                 if(pa[i]>pb[i]) return +1;
  11.         }
  12.         return 0;
  13. }

  14. int main()
  15. {
  16.         int buf[5][8] = { 10, 20, -12, 23,  55,  60, -51, 19
  17.                                         , 30, 41,  56, 61, 103, -10,   0, 12
  18.                                         , 10, 20,   8, 10,   7,  -7,  44, 55
  19.                                         , 10,  8,  11, -8,   0,   0,  77, 52
  20.                                         ,  8, 10,  99,  0,  -5,  11,  88, 33 };

  21.         qsort( buf, sizeof(buf)/sizeof(buf[0]), sizeof(buf[0]), &compare );
  22.        
  23.         for( unsigned int i=0; i<sizeof(buf)/sizeof(buf[0]); ++i )
  24.         {
  25.                 for( unsigned int j=0; j<sizeof(buf[0])/sizeof(buf[0][0]); ++j )
  26.                 {
  27.                         printf( "%5d ", buf[i][j] );
  28.                 }
  29.                 printf( "\n" );
  30.         }

  31.         return 0;
  32. }
复制代码

作者: hellioncu    时间: 2011-01-06 08:56
上面是别人用C++写的 , 但我想要用纯C写 , 不知道有人写过吗?

谢谢
shihyu 发表于 2011-01-06 01:14
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int compare(const void *arg1, const void *arg2)
  4. {
  5.         int i;

  6.         for (i = 0; i < 8; i++)
  7.         {
  8.                 if (((int *)arg1)[i] < ((int *)arg2)[i])
  9.                         return -1;
  10.                 else if (((int *)arg1)[i] > ((int *)arg2)[i])
  11.                         return 1;
  12.         }

  13.         return 0;
  14. }

  15. int main()
  16. {
  17.         int arrays[ 5 ][ 8 ] = {
  18.                 { 10, 20,-12, 23, 55, 60,-51, 19 },
  19.                 { 10, 20,  8, 10,  7, -7, 44, 55 },
  20.                 { 30, 41, 56, 61,103,-10,  0, 12 },
  21.                 { 8,  10, 99,  0, -5, 11, 88, 33 },
  22.                 { 10,  8, 11, -8,  0,  0, 77, 52 }
  23.         };

  24.         int i, j;

  25.         qsort(arrays, 5, sizeof(arrays[0]), compare);

  26.         for (i = 0; i < 5; i++)
  27.         {
  28.                 for (j = 0; j < 8; j++)
  29.                 {
  30.                         printf("%d, ", arrays[i][j]);
  31.                 }
  32.                 printf("\n");
  33.         }
  34. }
复制代码

作者: ecjtubaowp    时间: 2011-01-06 09:08
学习了,以前写过都忘了。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2