Created
November 18, 2015 05:02
-
-
Save stepancar/3aba0494d727f515e271 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #include <stdio.h> | |
| # include <stdlib.h> | |
| #include <locale.h> | |
| void sortq(void* *a1, long n, int(*cmp)(void*, void*)) | |
| { | |
| void* p; | |
| void* t; | |
| int i, j; | |
| if (n < 2) /*обязательно */ | |
| return; | |
| p = a1[n / 2]; /*опорный элемент - среднее значение*/ | |
| for (i = 0, j = n - 1;; i++, j--)/*i - номер левого элем-та,j - последнего .i++ - идем с первого вправо ,j— с правого - назад. ;; - чтобы работали оба цикла */ | |
| { | |
| while (cmp((void*)a1[i],(void*)p)) /*пока элемент левой части < опорного - идем вправо */ | |
| i++; | |
| while (cmp((void*)p,(void*)a1[j])) /*пока элемент правой части > опорного - идем влево */ | |
| j--; | |
| if (i >= j) /*когда i стал > j - прерываем цикл*/ | |
| break; | |
| t = a1[i];/*меняем левый и правый элем местами*/ | |
| a1[i] = a1[j]; | |
| a1[j] = t; | |
| } | |
| sortq(a1, i, cmp);/*вызываем функцию(указатель,левый элем)*/ | |
| sortq(a1 + i, n - i, cmp);/*вызываем функцию(указатель+левый элем,размер- левый элем*/ | |
| } | |
| int int_cmp(void *a, void *b){ return (int)a > (int)b; } | |
| int _tmain(int argc, _TCHAR* argv[]) | |
| { | |
| int n = 1000; | |
| int a1[1000]; | |
| long i, j; | |
| for (i = 0; i < n; i++) | |
| { | |
| a1[i] = rand(); | |
| } | |
| sortq((void* *)a1, n, int_cmp); /*вызываем sortq и выводим элементы */ | |
| for (i = 0; i < n; i++){ | |
| printf(" %i", a1[i]); | |
| } | |
| printf("\n \n "); | |
| getchar(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment