C Sorting Searching Sample Source Codes: 2d Example Insertion Sort (Linked List)
C Sorting Searching Sample Source Codes: 2d Example Insertion Sort (Linked List)
codes
2d example insertion sort [linked list]
#include <stdio.h>
#include <stdlib.h>
struct node {
int number;
struct node *next;
};
int main(void) {
struct node *current = NULL;
struct node *next = NULL;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i = 0;
return 0;
}
if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}
one = head;
two = head->next;
one->next = temp;
temp->next = two;
}
#include <stdio.h>
#define TRUE 0
#define FALSE 1
int main(void) {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;
printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
if(number == array[middle]) {
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}
if(bsearch == FALSE)
printf("-- Number Not found --\n");
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void) {
/* note, array needs to be sorted for bsearch... */
char *strings[] = { "bill", "chris", "jason", "randy", "trish" };
int i, asize, result;
i = asize = result = 0;
printf("\n");
return 0;
}
return 0;
}
#include <stdio.h>
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;
}
size -= 1;
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct lnode {
int data;
struct lnode *next;
} *head, *visit;
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;
}
tmp = *q;
while(visit != NULL) {
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}
/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != head->next) {
c = a = head;
b = a->next;
while(a != e) {
if(a->data > b->data) {
if(a == head) {
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else {
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}
Bubble sort [string array]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
#define N 2000
int main(void) {
char word[MAX];
char *x[N];
int n = 0;
int i = 0;
n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);
return(0);
}
tmp = *p;
*p = *q;
*q = tmp;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int array[MAXARRAY];
int i = 0;
heapsort(array, MAXARRAY);
return 0;
}
void heapbubble(int pos, int array[], int len) {
int z = 0;
int max = 0;
int tmp = 0;
int left = 0;
int right = 0;
z = pos;
for(;;) {
left = 2 * z + 1;
right = left + 1;
tmp = array[z];
array[z] = array[max];
array[max] = tmp;
z = max;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct lnode {
char *str;
struct lnode *next;
};
int main(void) {
char line[1024];
struct lnode *list;
list = NULL;
while((fgets(line, 1024, stdin)) != NULL)
list = insert(line, list);
print_list(list);
free_list(list);
return 0;
}
/* first, we handle the case where `data' should be the first element */
if(list == NULL || strcmp(list->str, data) > 0) {
/* apperently this !IS! the first element */
/* now data should [be|becomes] the first element */
p->next = list;
return p;
} else {
/* search the linked list for the right location */
q = list;
while(q->next != NULL && strcmp(q->next->str, data) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return list;
}
}
while(list != NULL) {
p = list->next;
free(list);
list = p;
}
}
#include <stdio.h>
int main(void) {
float arr1[5] = {4.3, 6.7, 2.8, 8.9, 1.0};
float arr2[5] = {4.3, 6.7, 2.8, 8.9, 1.0};
int i = 0;
isort(arr2, 5);
printf("\nBefore\tAfter\n--------------\n");
return f;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int number;
struct node *next;
};
int main(void) {
struct node *head;
struct node *current;
struct node *next;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i;
head = NULL;
/* insert some numbers into the linked list */
for(i = 0; i < 10; i++)
head = addnode(test[i], head);
/* done... */
return 0;
}
if(tnode != NULL) {
tnode->number = number;
tnode->next = next;
}
return tnode;
}
if(head_one == NULL)
return head_two;
if(head_two == NULL)
return head_one;
return head_three;
}
#include <stdio.h>
#include <stdlib.h>
#define MAXARRAY 10
int main(void) {
int array[MAXARRAY];
int i = 0;
/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;
printf("\n");
printf("\n");
return 0;
}
if(low == high)
return;
merge1 = 0;
merge2 = pivot - low + 1;
#include <stdio.h>
#include <stdlib.h>
struct lnode {
int number;
struct lnode *next;
};
int main(void) {
struct lnode *new = NULL;
int i = 0;
while(a != NULL) {
c = b, b = a, a = a->next;
b->next = c;
}
*n = b;
}
printf("\n");
}
#include <stdio.h>
#define MAXARRAY 10
int main(void) {
int array[MAXARRAY] = {0};
int i = 0;
return 0;
}
/* partition */
do {
/* find member above ... */
while(arr[i] < z) i++;
if(i <= j) {
/* swap two elements */
y = arr[i];
arr[i] = arr[j];
arr[j] = y;
i++;
j--;
}
} while(i <= j);
/* recurse */
if(low < j)
quicksort(arr, low, j);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *str;
};
int main(void) {
struct node **strarray = NULL;
int i = 0, count = 0;
char line[1024];
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char line[1024];
char *line_array[1024];
int i = 0;
int j = 0;
while((fgets(line, 1024, stdin)) != NULL)
if(i < 1024)
line_array[i++] = strdup(line);
else
break;
sortstrarr(line_array, i);
while(j < i)
printf("%s", line_array[j++]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sortstrarr(strarray, strcount);
/* free mem... */
for(i = 0; i < strcount; i++)
free(strarray[i]);
free(strarray);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct lnode {
int data;
struct lnode *next;
} *head, *visit;
head = newnode;
printf("Before selection sort:\n");
llist_print();
printf("After selection sort:\n");
llist_selection_sort();
llist_print();
return 0;
}
temp = *q;
void llist_selection_sort(void) {
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *d = NULL;
struct lnode *tmp = NULL;
a = c = head;
while(a->next != NULL) {
d = b = a->next;
while(b != NULL) {
if(a->data > b->data) {
/* neighboring linked list node */
if(a->next == b) {
if(a == head) {
a->next = b->next;
b->next = a;
tmp = a;
a = b;
b = tmp;
head = a;
c = a;
d = b;
b = b->next;
} else {
a->next = b->next;
b->next = a;
c->next = b;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
}
} else {
if(a == head) {
tmp = b->next;
b->next = a->next;
a->next = tmp;
d->next = a;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
head = a;
} else {
tmp = b->next;
b->next = a->next;
a->next = tmp;
c->next = b;
d->next = a;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
}
}
} else {
d = b;
b = b->next;
}
}
c = a;
a = a->next;
}
}
#include <stdio.h>
#define MAXARRAY 10
int main(void) {
int array[MAXARRAY] = {0};
int i = 0;
/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;
return 0;
}
return;
}
Ssort, selection sort [array]
#include <stdio.h>
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");
selection_sort(arr, 10);
printf("after:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tnode {
char *str;
struct tnode *left;
struct tnode *right;
};
int main(void) {
char line[1024];
struct tnode *root;
root = NULL;
while((fgets(line, 1024, stdin)) != NULL)
insert(&root, line);
print(root);
return 0;
}
/* call by reference .. ! */
void insert(struct tnode **p, char *value) {
if(!*p) {
*p = (struct tnode *)malloc(sizeof(struct tnode));
(*p)->left = (*p)->right = NULL;
(*p)->str = strdup(value);
return;
}