FAQ по C/C++/Visual C++
Работа с сетью Сортировка эллементов массива Рекурсивная сортировка |
Составители: SUnteXx, Leprecon |
Сортировка эллементов массива | |||
void QuickSort(double* array, int a, int b) { int A = a; int B = b; double mid; if ( b > a) { /* Arbitrarily establishing partition element as the midpoint of * the array. */ mid = array[ ( a + b ) / 2 ]; // loop through the array until indices cross while( A <= B ) { /* find the first element that is greater than or equal to * the partition element starting from the left Index. */ while( ( A < b ) && ( array[A] < mid )) ++A; /* find an element that is smaller than or equal to * the partition element starting from the right Index. */ while( ( B > a ) && ( array[B] > mid )) --B; // if the indexes have not crossed, swap if( A <= B ) { double T; T = array[A]; array[A] = array[B]; array[B] = T; ++A; --B; } } /* If the right index has not reached the left side of array * must now sort the left partition. */ if( a < B ) QuickSort( array, a, B ); /* If the left index has not reached the right side of array * must now sort the right partition. */ if( A < b ) QuickSort( array, A, b ); } } void QuickSort(double* array, int n) { QuickSort(array, 0, n-1); } |
Рекурсивная сортировка|||
|
FAQ составлен по материалам Форума на Исходниках.Ру. Copyright © 2002 by Sources.ru. All rights reserved. |