Skip to content

Instantly share code, notes, and snippets.

@MShrimp4
Created November 8, 2018 01:02
Show Gist options
  • Save MShrimp4/92740eedaab5432723767a5b4f56f1a7 to your computer and use it in GitHub Desktop.
Save MShrimp4/92740eedaab5432723767a5b4f56f1a7 to your computer and use it in GitHub Desktop.
simple sort
#include <stdio.h>
#include <stdbool.h>
bool swap(int * a, int * b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
bool sortswap(int * a, int * b){
if(*a>=*b){
return true;
}
else{
swap(a,b);
return false;
}
}
bool sort_step(int len, int arr[]){
int is_sorted = true;
for(int i = 0; i<len-1; i++){
is_sorted = is_sorted && sortswap(&arr[i],&arr[i+1]);
}
return is_sorted;
}
void sort(int len, int arr[]){
while (!sort_step(len, arr));
return;
}
int main(void){
int num[5];
int size = sizeof(num)/sizeof(int);
printf("5개의 숫자를 입력하시오:");
for(int i = 0; i<size;i++) scanf("%d",&num[i]);
sort(size, num);
for(int i = 0; i<size;i++) printf("%d%s",num[i],(i == size -1)?"":",");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment