Posts

Showing posts with the label bubble sort

string array sorting using bubble sort algorithm

Bubble sort in string array #include <stdio.h> #include <conio.h> #include <string.h> #define MAX 50 #define N 2000 void sort_words(char *x[], int y); void swap(char **, char **); int main(void) { char word[MAX]; char *x[N]; int n = 0; int i = 0; for(i = 0; scanf("%s", word) == 1; ++i) { if(i >= N) printf("Limit reached: %d\n", N), exit(1); x[i] = calloc(strlen(word)+1, sizeof(char)); strcpy(x[i], word); } n = i; sort_words(x, n); for(i = 0; i < n; ++i) printf("%s\n", x[i]); return(0); } void sort_words(char *x[], int y) { int i = 0; int j = 0; for(i = 0; i < y; ++i) for(j = i + 1; j < y; ++j) if(strcmp(x[i], x[j]) > 0) swap(&x[i], &x[j]); } void swap(char **p, char **q) { char *tmp; tmp = *p; *p = *q; *q = tmp; } << Go to Index Page >>

bubble sort using user defined function

bubble sort #include <stdio.h> void bubble_sort(int a[], int size); int main(void) { int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9}; int i = 0; printf("before:\n"); for(i = 0; i < 10; i++) printf("%d ", arr[i]); printf("\n"); bubble_sort(arr, 10); printf("after:\n"); for(i = 0; i < 10; i++) printf("%d ", arr[i]); printf("\n"); return 0; } void bubble_sort(int a[], int size) { int switched = 1; int hold = 0; int i = 0; int j = 0; size -= 1; for(i = 0; i < size && switched; i++) { switched = 0; for(j = 0; j < size - i; j++) if(a[j] > a[j+1]) { switched = 1; hold = a[j]; a[j] = a[j + 1]; a[j + 1] = hold; } } }

bubble sort with struct using c programming

Bubble sort - linked list #define MAX 10 struct lnode { int data; struct lnode *next; } *head, *visit; /* add a new entry to the linked list */ void llist_add(struct lnode **q, int num); /* preform a bubble sort on the linked list */ void llist_bubble_sort(void); /* print the entire linked list */ void llist_print(void); int main(void) { /* linked list */ struct lnode *newnode = NULL; int i = 0; /* a general counter */ /* load some random values into the linked list */ for(i = 0; i < MAX; i++) { llist_add(&newnode, (rand() % 100)); } head = newnode; printf("Before bubble sort:\n"); llist_print(); printf("After bubble sort:\n"); llist_bubble_sort(); llist_print(); return 0; } /* adds a node at the end of a linked list */ void llist_add(struct lnode **q, int num) { struct lnode *tmp; tmp = *q; /* if the list is empty, create first node */ if(*q == NULL) { *q = malloc(sizeof(struct lnode)); tmp = *q; } else {

bubble sort using user defined function

A bubble sort routine # include # include void bubblesort(int array[],int size); void main() { int values[10],j; for(j=0;j<10;j++) values[j] = rand()%100; /*unsorted*/ printf("\nUnsorted values.\n"); for(j=0;j<10;j++) printf("%d ",values[j]); /*sorted*/ printf("\nSorted values.\n"); bubblesort(values,10); for(j=0;j<10;j++) printf("%d ",values[j]); } void bubblesort(int array[],int size) { int tmp ,i,j; for(i = 0;i for(j=0;j < size;j++) if(array[i] < array[j]) { tmp = array[i]; array[i] = array[j]; array[j] = tmp; } }