From 7a7656ad2609592f835cd09409b7185b6dad4838 Mon Sep 17 00:00:00 2001 From: kemasama Date: Thu, 28 Apr 2022 19:53:55 +0900 Subject: [PATCH] add: quick sort split array --- README.md | 1 + quick_sort/array_quick_sort | Bin 0 -> 33949 bytes quick_sort/array_quick_sort.c | 118 ++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100755 quick_sort/array_quick_sort create mode 100644 quick_sort/array_quick_sort.c diff --git a/README.md b/README.md index b673e47..f40ba30 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ GCCを使用してコンパイルしています。 - 2022/04/22 q_sort - 2022/04/26 quick_sort +- 2022/04/28 array_quick_sort #### 処理内容 diff --git a/quick_sort/array_quick_sort b/quick_sort/array_quick_sort new file mode 100755 index 0000000000000000000000000000000000000000..7e5c5e7a978f19b974bfb76d3c74e852b9bd4993 GIT binary patch literal 33949 zcmeI5e{56N702)Mi(5N!8%m<4BoKRLK&U#H*rauo6w?p^?Nm@eT42D%#11j}iS2ZR z1m?}60yW*1meIAe%=C}dPRAB?>-M6l3&_*7t(&?qiAk+gQ8a0GI?(n`9 zY}(Sgr?qW6Sr-;Hd7*g|+Jvn;VJ1OHsz@0N9gW!vs&ao5 z>#fa<+7n2pea)8&5ub0PA!Rf+crZF|D)+Zxqvh{_J%aR7e=ge}7^)&=M>G}*_Z^zo z7w-2Z+h2{{PkL!7^-Xpu2m8Jn?hDFrUuVD47y6s9{q3-wkY4V??ot)y;kytiH*eat zNw#c%VyBf2zkLW$t5R0$Fp^NN7D1#u)L)W@kMviJOZct%n5xPhc)IL1RkzOBm$p6e zc4WLQ9ZTlJXJdKFnYIZ~Dl$4?^c+?O-&rwkLdWZE&b32%Sm;gyQ)5q3yrB zGP-WVOE*@{j-T38u?{vL#M%>P>mJo6_Cm}$9>7!WPw7H5VSnUjbF*!*D_Zv<2=&pP z^`I9&Bkfi!h-c?eG#2Vz^XMAH6+VbTC@Uf4|Fhsj`B`o2#EZ%@h+02^99Tg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HB`Wja!@!2f8O{#=<(?|YzkJka|c=-m!NnziYYjXvj`t=Tx0d-kNa?eO0YkDOlqSY&zo%E_j!$9}xyXJ7c< z<%?hY!Rzh)zshx9s`%6Nbhl%ByR$IzlaXU5-+A-tt*0M+^^^L22Ugr1e&g5Y&USgs z(G!jDG@pCx_aBD7bv|47<_mYNo1WR1+xf#2fBEM6nv2i8J6bi8+_2)2`q102KJ}w# z9!|ac-r3`y +#include +#include + +int +array_copy( + int *array1, + int *array2, + int array2_size +) +{ + int i; + + for ( i = 0 ; i < array2_size ; i++ ) { + array1[i] = array2[i]; + } + + return 0; +} + +int +quick_sort( + int *array, + int left, + int right +) +{ + int *group_small; + int *group_large; + int small_index = 0; + int small_size = 0; + int large_index = 0; + int large_size = 0; + + int i = 0; + int range = right - left; + int pivot; + int pivot_index = 0; + + if ( left >= right ) { + return -1; + } + + group_small = (int *)malloc(sizeof(int) * range); + if ( group_small == NULL ) { + return -1; + } + + group_large = (int *)malloc(sizeof(int) * range); + if ( group_large == NULL ) { + free(group_small); + return -1; + } + + pivot = array[left]; + + for ( i = left + 1 ; i <= right ; i++ ) { + if ( array[i] < pivot ) { + group_small[small_index] = array[i]; + small_index++; + small_size++; + } else { + group_large[large_index] = array[i]; + large_index++; + large_size++; + } + } + + pivot_index = left + small_size; + + array_copy(array + left, group_small, small_size); + array[pivot_index] = pivot; + array_copy(array + pivot_index + 1, group_large, large_size); + + free(group_small); + free(group_large); + + quick_sort(array, left, pivot_index - 1); + quick_sort(array, pivot_index + 1, right); + + return 0; +} + +int +main( + int argc, + char *argv[] +) +{ + int i; + + int length = 200; + int rand_limit = 100; + + if ( argc > 1 ) { + length = atoi(argv[1]); + } + + int array[length]; + + srand((int)time(NULL)); + + for ( i = 0 ; i < length ; i++ ) { + array[i] = rand() % rand_limit + 1; + } + + time_t start_at = time(NULL); + quick_sort(array, 0, length - 1); + time_t end_at = time(NULL); + + for ( i = 0 ; i < length ; i++ ) { + printf("%d\n", array[i]); + } + + printf("elapsed: %lds\n", end_at - start_at); + + return 0; +}