Created
March 10, 2017 12:45
-
-
Save lixin9311/d6960e15df950aaf2478e03ea0c939e4 to your computer and use it in GitHub Desktop.
This file contains 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 <stdlib.h> | |
#include <assert.h> | |
#define SIZE 30 | |
void bubble_sort(int arr[], int len, int direction) { | |
int i, j, temp; | |
for (i = 0; i < len - 1; i++) | |
for (j = 0; j < len - 1 - i; j++) | |
if ((arr[j] > arr[j + 1]) == direction) { | |
temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
} | |
} | |
int bubble_spec(int arr[], int len, int num) { | |
int p1, p2, temp; | |
for (p1 = 0, p2 = 0; p2 < len; p2++) { | |
if (arr[p2] % num == 0) { | |
temp = arr[p1]; | |
arr[p1] = arr[p2]; | |
arr[p2] = temp; | |
p1++; | |
} | |
} | |
return p1; | |
} | |
void print_line(int arr[], int len) { | |
int i; | |
for (i = 0; i < len -1; i++) { | |
printf("%d ", arr[i]); | |
} | |
printf("%d\n", arr[len-1]); | |
} | |
int main(void) { | |
int a[SIZE], i, *pa, len = SIZE; | |
for (i = 0; i < SIZE; i++) { | |
a[i] = rand()%(500-50+1); | |
} | |
printf("Original Array:\n"); | |
print_line(a, SIZE); | |
printf("Bubble Sort:\n"); | |
bubble_sort(a, SIZE, 1); | |
print_line(a, SIZE); | |
len = bubble_spec(a, SIZE, 2); | |
printf("Bubble odd:\n"); | |
print_line(a, SIZE); | |
bubble_spec(a, len, 3); | |
bubble_spec(a+len, SIZE-len, 3); | |
printf("Final:\n"); | |
print_line(a, SIZE); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment