C++/STL Algorithms Sorting/qsort — различия между версиями


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:21, 25 мая 2010

Using qsort().

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int sortFunction( const void *intOne, const void *intTwo);
const int length = 10;  
int main(void)
{
   int i,intArray[length];
   for (i = 0; i < length; i++)
   {
      cout << "Enter a number: ";
      cin >> intArray[i];
   }
   cout << "\n";
   qsort((void *)intArray, length, sizeof(intArray[0]), sortFunction);
   for (i = 0; i < length; i++)
      cout << "intArray [" << i << "]: " << intArray[i] << endl;
   cout << "Done." << endl;
 return 0;
}
int sortFunction( const void *a, const void *b)
{
   int intOne = *((int*)a);
   int intTwo = *((int*)b);
   if (intOne < intTwo)
      return -1;
   if (intOne == intTwo)
      return 0;
   return 1;
}