Created
April 6, 2019 07:03
-
-
Save kohnakagawa/76d7e62a65dfe6a1694c50cfc2cda326 to your computer and use it in GitHub Desktop.
2分くらいで書いたsort関数
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 <stdio.h> | |
#include <string.h> | |
void bubble(char* p) { | |
const size_t len = strlen(p); | |
for (size_t i = 0; i < len; i++) { | |
for (size_t j = i+1; j < len; j++) { | |
const char tmp0 = p[i]; | |
const char tmp1 = p[j]; | |
if (tmp0 > tmp1) { | |
p[i] = tmp1; | |
p[j] = tmp0; | |
} | |
} | |
} | |
} | |
void insertion_sort(char* p) { | |
const int len = strlen(p); | |
for (int i = 1; i < len; i++) { | |
int j = i; | |
while ((j > 0) && (p[j-1] > p[j])) { | |
const char tmp = p[j-1]; | |
p[j-1] = p[j]; | |
p[j] = tmp; | |
j--; | |
} | |
} | |
} | |
int main() { | |
char str[] = "431250"; | |
insertion_sort(str); | |
printf("%s\n", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment