Professional Documents
Culture Documents
2. 3.
4.
5. 6.
7. 8. 9.
Bubble Sort Merge Sort Insertion Sort Selection Sort Quick Sort WAP in C for following Searching Methods Linear Search Binary Search WAP in C for array implementation of Stack Queue Circular Queue Linked List WAP in C using dynamic memory allocation of Stack Queue Circular Queue Linked List WAP in C for implementation of Binary Tree. WAP in C for Tree Traversal Pre-Order In-Order Post-Order WAP in C for Graph Traversal Breadth First Search Depth First Search WAP in C for Minimum Cost Spanning Tree WAP in C for Shortest Path Problem
Page 2
1. WAP in C for following Sorting Methods Bubble Sort Merge Sort Insertion Sort Selection Sort Quick Sort Program: Bubble Sort: Algorithm [1] Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. [2] If at least one swap has been done, repeat step 1. Time Complexity Best case: Average case: Worst case: Code Snippet: #include <stdio.h> int* bubble_sort(int i_store[], int i_size); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("\n\n\n"); printf("\t\t\tWAP of Bubble sort."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = bubble_sort(ip_store, i_store_size); O (n) time O (n2) time O (n2) time
Page 3
Page 15
void linear_search(int* i_storage, int i_srch_item, int i_num_item) { int i_count; bool b_flag = false; for (i_count = 0; i_count < i_num_item; ++i_count) { if (i_storage[i_count] == i_srch_item) { b_flag = true; printf("Item (%d) is found at position: %d", i_srch_item, i_count); break; } } if (!b_flag) Saket Kr. Pathak Page 16
bool check_item_order(int* i_storage) { //Function to check the Item - List is in Sorted order. return true; } void recursive_bin_search(int* i_storage, int i_srch_item, int i_low, int i_mid, int i_hi) { if(i_low < i_hi) { if(i_storage[i_mid] == i_srch_item) { printf("Item (%d) is found at position: %d", i_srch_item, i_mid); } else if(i_storage[i_mid] > i_srch_item) { i_hi = i_mid; i_mid = (i_low + i_hi)/2;
Page 18
Page 20
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int push_item(int i_item) { if(i_top == (STACK_SIZE - 1)) { printf("\n\tStack Overflow."); return 0; } else Saket Kr. Pathak Page 21
//Check
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int push_item(int i_item) { if(i_rear == (QUEUE_SIZE - 1)) { printf("\n\tQueue Overflow."); return 0; } else Saket Kr. Pathak Page 26
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); Saket Kr. Pathak Page 29
Page 32
Page 33
struct node { int i_val; int i_next_idx; }ll_node[LINKED_LIST_SIZE]; int i_ll_size = 0; int main() { int i_check = set_argument(); if(i_check == 0) printf("\n\n\n\tInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("\n\n\n"); getch(); return 0; } int set_argument(void) { printf("\n\t-------------------------------------------------\n"); Saket Kr. Pathak Page 34
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List.\n"); int i_item = 0; int i_indx = 0; printf("\n\tPlease Enter the item: "); scanf("%d", &i_item); printf("\n\tPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1; Saket Kr. Pathak Page 35
struct node { int i_prev_idx; int i_val; int i_next_idx; }ll_node[LINKED_LIST_SIZE]; int i_ll_size = 0; int main() { int i_check = set_argument(); Saket Kr. Pathak Page 40
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); Saket Kr. Pathak Page 41
Page 46
Page 47
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Stack."); int i_item = 0; printf("\n\tPlease Enter the item: "); scanf("%d", &i_item); int i_check = push_item(i_item); if(i_check == 1) Saket Kr. Pathak Page 49
~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Queue: Code Snippet: #include <stdio.h> #include <stdbool.h> #define QUEUE_SIZE 1024 int i_front = -1; int i_rear = -1; int *queue; int main() { int i_check = set_Argument(); if(i_check == 0) printf("\n\n\n\tInvalid input."); else { int i_state; queue = (int*)malloc(sizeof(int) * QUEUE_SIZE); do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Check for Exit. Saket Kr. Pathak Page 52
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int push_item(int i_item) { Saket Kr. Pathak Page 53
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); Saket Kr. Pathak Page 57
struct node { int i_val; int i_next_idx; }*ll_node; int i_ll_size = 0; int main() { int i_check = set_argument(); if(i_check == 0) printf("\n\n\n\tInvalid input."); else { int i_state; ll_node = (struct node*)malloc(sizeof(int) * LINKED_LIST_SIZE); do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("\n\n\n"); getch(); return 0; Saket Kr. Pathak Page 61
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List.\n"); int i_item = 0; int i_indx = 0; printf("\n\tPlease Enter the item: "); Saket Kr. Pathak Page 62
Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");
int i_choice; printf("\n\n\tPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List.\n"); int i_item = 0; int i_indx = 0; printf("\n\tPlease Enter the item: "); scanf("%d", &i_item); printf("\n\tPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1; else return 0; break; } case 2: { printf("\n\tTo Pop Item from Queue."); int i_check = del_option(); Saket Kr. Pathak Page 69
Page 74
Page 78
Page 81
Page 82
Page 85
7. WAP in C for Graph Traversal Breadth First Search Depth First Search Program: Breadth First Search: Algorithm [1] Enqueue the root node. [2] Dequeue a node and examine it a. If the element sought is found in this node, quit the search and return a result. b. Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered. [3] If the queue is empty, every node on the graph has been examined quit the search and return "not found". [4] If the queue is not empty, repeat from Step 2 Time Complexity The total time for initializing is O (n) and the total time for the queuing operations is O (n) because each node is put on the queue exactly once. The total time in the main loop is O (e) because we look at each edge at most twice, once from each direction. This gives a time complexity of O (n + e). Code Snippet: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int int int int int **i_adjacency_matrix; *i_nodes_visit; *i_bfs_path; i_no_nodes; flag, n_flag = -1; get_graph_input(void); initialize_nodes(void); breadth_first_search(void); check_node(int); Page 87
Page 89
int **i_adjacency_matrix; int *i_nodes_visit; int i_no_nodes; bool get_graph_input(void); Saket Kr. Pathak Page 90
Page 93
Page 96
Page 100