From f0f5ffaf5ec523def8fccf1cf78920fdae9f880c Mon Sep 17 00:00:00 2001 From: kemasama Date: Mon, 25 Apr 2022 20:16:09 +0900 Subject: [PATCH] fix: quick sort --- quick_sort/q_sort | Bin 0 -> 33763 bytes quick_sort/q_sort.c | 52 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100755 quick_sort/q_sort diff --git a/quick_sort/q_sort b/quick_sort/q_sort new file mode 100755 index 0000000000000000000000000000000000000000..bb441fccc97f89332a8695dc353215769d6976fd GIT binary patch literal 33763 zcmeI5e{54#6vyxD+oWS;9d3$>(jo#v!1+7=nAkBEKoAyKWHHlgS<6<(ezq0nP^+kk z(Zmgw$R8wh3bOITOhU|x(P3u%!$fgJ(HJ!o|FA`k5>dD5tlx9rZP%4W5{-#}oRgfs zbI!f@+6;LV5OO}`2U$$;7MJI|H zJTY{hM|nO&M=2}HDk~;7tMTrMF`@lzgp*1=qzwAD2FbD{GhgHmYcrMD0}1)Guasno zaLR@uWgr;Z7)XUO^9?Vt@?E2Qke{BIv>@Ig#2{L%q?Z8VT-@PEBy_d8qqN`U$4$ru9J|TnIk%gvfAV}T^i|@v#%oVp1cUM=W&FNf zy>iW}vSll+RSBy8g!>IX$v1j1wW)R$72;-$p!Lx>+UHH$-vz%GKC!tBn*XE_Q*r$W z*R8nbLX!+(Dxo!!W?Al}zr7egWs;UW#9UCi&x%5UmZHu6jYZzp&0hE*lBDo4m@$#pH*l8`2!YoXH z2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5U;<2l z2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60VeQ2Cy>}K28SGC-h$xPs6_Qd;)rR@V@7^wlOQ+M#s zL`7;`t{T@&IaA}%H#IKz{Be5yQOb3XAyls9d?F|3d1Ygq0sGBF;-%DF*1pxax>0Kl zsxRit9CsMwhOoA=wxvDbazQ?EVnge(S?h^l&t2el8u0^^3tW;hTE8t@G=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sort/q_sort.c index 66c4417..b7072e6 100644 --- a/quick_sort/q_sort.c +++ b/quick_sort/q_sort.c @@ -60,6 +60,7 @@ int right return j; } + /*********************************** クイックソートを行う ***********************************/ @@ -82,6 +83,55 @@ int right return 0; } +/*********************************** + クイックソートを行う +***********************************/ +int +quick_sort( +int *array, +int left, +int right +) +{ + int right_index, left_index, pivot; + + if ( left >= right ) + { + return -1; + } + + left_index = left; + right_index = right; + pivot = left; + + for ( ; left_index < right_index ; ) + { + for ( ; array[left_index] < array[pivot] ; ) + { + left_index++; + } + + right_index--; + + for ( ; array[right_index] > array[pivot] ; ) + { + right_index--; + } + + if ( left_index < right_index ) + { + swap(&array[right_index], &array[left_index]); + } + } + + swap(&array[pivot], &array[right_index]); + + quick_sort(array, left, right_index - 1); + quick_sort(array, right_index + 1, right); + + return 0; +} + /*********************************** メイン関数 ***********************************/ @@ -102,7 +152,7 @@ main() int length = sizeof(array) / sizeof(int); printf("Length: %d\n", length); - q_sort(array, 0, length); + quick_sort(array, 0, length); int i ;