From c5f3cb176b3b0331165482c462695505353afca4 Mon Sep 17 00:00:00 2001 From: Ann-SB Date: Wed, 30 Oct 2019 20:46:31 +0530 Subject: [PATCH 1/7] Selection sort implemented in C --- Sorting Algorithm/Selection_Sort.c.txt | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Sorting Algorithm/Selection_Sort.c.txt diff --git a/Sorting Algorithm/Selection_Sort.c.txt b/Sorting Algorithm/Selection_Sort.c.txt new file mode 100644 index 0000000..54649c3 --- /dev/null +++ b/Sorting Algorithm/Selection_Sort.c.txt @@ -0,0 +1,37 @@ +/* +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. +The algorithm maintains two subarrays in a given array. + +1) The subarray which is already sorted. +2) Remaining subarray which is unsorted. + +In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. +*/ + + +#include + +void main() +{ int a[100],n,i,j,temp; //'a' is an integer array of size 100 + printf("Enter no. of elements: "); + scanf("%d",&n); //'n' stores the number of elements in the array + printf("Enter array:"); + for(i=0;ia[j]) + { temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + } + } + + printf("Sorted array :"); + for(i=0;i Date: Wed, 30 Oct 2019 20:50:11 +0530 Subject: [PATCH 2/7] Added sorting algorithms to the list --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index eb5fba2..c9711d4 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,9 @@ Use this repo to study or review your knowledge and don't forget to star and col - Binary Search - Linear Search +## Sorting Algorithms +- Bubble Sort +- Selection Sort + # How to contribute Please comment your code thoroughly as to make it possible for anyone to understand. If possible, check your code using unit tests. Avoid all the bad implementations, make your code as clean as possible. After that, find the folder that fits the category of your code and submit a PR. Star this repo if the information here is useful to you.q From c207139babce8a66d8e1166e52bf54305cb28c7d Mon Sep 17 00:00:00 2001 From: Ann-SB Date: Thu, 31 Oct 2019 23:42:49 +0530 Subject: [PATCH 3/7] Selection sort implementation in C --- Searching Algorithms/Selection_Sort.c | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Searching Algorithms/Selection_Sort.c diff --git a/Searching Algorithms/Selection_Sort.c b/Searching Algorithms/Selection_Sort.c new file mode 100644 index 0000000..248b6ff --- /dev/null +++ b/Searching Algorithms/Selection_Sort.c @@ -0,0 +1,38 @@ +/* +The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. +The algorithm maintains two subarrays in a given array. + +1) The subarray which is already sorted. +2) Remaining subarray which is unsorted. + +In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. +*/ + + +#include + +void main() +{ int a[100],n,i,j,temp; //'a' is an integer array of size 100 + printf("Enter no. of elements: "); + scanf("%d",&n); //'n' stores the number of elements in the array + printf("Enter array:"); + for(i=0;ia[j]) + { temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + } + } + + printf("Sorted array :"); + for(i=0;i Date: Thu, 31 Oct 2019 23:46:46 +0530 Subject: [PATCH 4/7] Heap sort implementation in C --- Sorting Algorithm/Heap_Sort.c | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Sorting Algorithm/Heap_Sort.c diff --git a/Sorting Algorithm/Heap_Sort.c b/Sorting Algorithm/Heap_Sort.c new file mode 100644 index 0000000..83e68e4 --- /dev/null +++ b/Sorting Algorithm/Heap_Sort.c @@ -0,0 +1,65 @@ +/* +Heap sort is a comparison based sorting technique based on Binary Heap data structure. +It is similar to selection sort where we first find the maximum element and place the maximum element at the end. +The same process is repeated for remaining elements. + +A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes. +The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array. + +Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based representation is space efficient. +If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0). + +Heap Sort Algorithm for sorting in increasing order: +1. Build a max heap from the input data. +2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree. +3. Repeat above steps while size of heap is greater than 1. +*/ + +#include + +void swap(int *a, int *b) //function to swap 2 values +{ + int t=*b; + *b=*a; + *a=t; +} + +void buildHeap(int a[], int curr, int size) //function to build heap +{ + int left = 2 * curr + 1, right = 2 * curr + 2, largest = curr; + if(left < size) + largest = a[largest] > a[left] ? largest : left; + if(right < size) + largest = a[largest] > a[right] ? largest : right; + if( largest != curr ) + { + swap(&a[largest], &a[curr]); + buildHeap(a,largest,size); + } +} + +void HeapSort(int a[], int size) //function to sort +{ + int i; + for(i= size/2 - 1;i >= 0; --i) + buildHeap(a,i,size); + for (i = size - 1; i >= 0; i--) + { + swap(&a[0], &a[i]); + buildHeap(a, 0, i); + } +} + +void main() +{ + int size, a[200],i; + printf("Enter the size of the array :"); + scanf("%d",&size); + printf("Enter the elements :\n"); + for(i = 0; i < size; ++i) + scanf("%d",&a[i]); + HeapSort(a,size); + printf("Sorted Array : \n"); + for(i = 0; i < size; ++i) + printf("%d ",a[i]); +} From 3e1638b01c498f00d6c2c7b5c15a2994ec0d934c Mon Sep 17 00:00:00 2001 From: Ann-SB Date: Thu, 31 Oct 2019 23:47:43 +0530 Subject: [PATCH 5/7] Added heap sort algorithm --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c9711d4..86a4b0a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Use this repo to study or review your knowledge and don't forget to star and col ## Sorting Algorithms - Bubble Sort - Selection Sort +- Heap Sort # How to contribute Please comment your code thoroughly as to make it possible for anyone to understand. If possible, check your code using unit tests. Avoid all the bad implementations, make your code as clean as possible. After that, find the folder that fits the category of your code and submit a PR. Star this repo if the information here is useful to you.q From 2eae836ed04d91e5ac28097777a9041bd80bafba Mon Sep 17 00:00:00 2001 From: Ann-SB Date: Thu, 31 Oct 2019 23:57:55 +0530 Subject: [PATCH 6/7] Circular linked list in C --- Circular_Linked_List.c | 100 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Circular_Linked_List.c diff --git a/Circular_Linked_List.c b/Circular_Linked_List.c new file mode 100644 index 0000000..e53001f --- /dev/null +++ b/Circular_Linked_List.c @@ -0,0 +1,100 @@ +#include +#include + +struct node +{ int data; + struct node * link; +}*f=NULL,*e=NULL,*temp; //'f' points to first and 'e' points to last node + +void insert(int data) +{ + temp=(struct node *)malloc(sizeof(struct node)); //allocate node + temp->data=data; //put in data + if(f==NULL) //if list is empty make 'f' and 'e' point to temp + f=e=temp; + else //if list is not empty + { e->link=temp; + e=temp; + } + e->link=f; +} + + +void del(int data) +{ int fl=0; + struct node *t; + if(f==NULL) + printf("Empty\n"); + else if(f==e) + { t=f; + f=e=NULL; + free(t); + } + else if(f->data==data) + { t=f; + f=f->link; + e->link=f; + free(t); + } + else + { temp=f; + while(1) + { t=temp->link; + if(t!=f && t->data==data) + { temp->link=t->link; + if(t==e) + e=temp; + free(t); + fl=2; + break; + } + else if(t==f) + break; + temp=temp->link; + fl=1; + } + if(fl==1) + printf("Element not found\n"); + } +} + +void display() +{ int fl=0; + if(f==NULL) + printf("Empty\n"); + else + { temp=f; + while(1) + { printf("%d\n",temp->data); + if(f==e||(temp->link==f && fl==1)) + break; + temp=temp->link; + fl=1; + } + } +} + +void main() +{ int ch,prev,data; + + while(1) + { printf("\nMENU \n1.Insertion \n2.Deletion \n3.Exit \nEnter choice:"); + scanf("%d",&ch); + if(ch==1) + { printf("Enter element to be inserted:"); + scanf("%d",&data); + insert(data); + display(); + } + else if(ch==2) + { printf("Enter element to be deleted:"); + scanf("%d",&data); + del(data); + display(); + } + else if(ch==3) + break; + else + printf("Invalid choice\n"); + } +} From 2a4b5b3b67f0446c4384ff22a1028addecffbfd0 Mon Sep 17 00:00:00 2001 From: Ann-SB Date: Fri, 1 Nov 2019 00:01:21 +0530 Subject: [PATCH 7/7] Added doubly linked list in C --- Doubly_Linked_List.c | 166 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 Doubly_Linked_List.c diff --git a/Doubly_Linked_List.c b/Doubly_Linked_List.c new file mode 100644 index 0000000..2a928ad --- /dev/null +++ b/Doubly_Linked_List.c @@ -0,0 +1,166 @@ +#include +#include + +struct node +{ int data; + struct node *pre,*next; +}*f=NULL,*e=NULL,*temp; + +void in_beg(int data) +{ /* 1. allocate node */ + temp=(struct node *)malloc(sizeof(struct node)); + /* 2. put in the data */ + temp->data=data; + /* 3. Make next of new node as head and previous as NULL */ + temp->pre=NULL; + if(f==NULL) //in case list is currently empty + { temp->next=NULL; + e=temp; + } + else + { temp->next=f; + f->pre=temp; + } + /* 5. move the head to point to the new node */ + f=temp; +} + +void in_end(int data) +{ temp=(struct node *)malloc(sizeof(struct node)); + temp->data=data; + temp->next=NULL; + if(f==NULL) + { temp->pre=NULL; + f=temp; + } + else + { temp->pre=e; + e->next=temp; + } + e=temp; +} + +void in_mid(int data,int prev) +{ int fl=0; + struct node *t; + temp=(struct node *)malloc(sizeof(struct node)); + temp->data=data; + if(f==NULL) + printf("Empty"); + else + { t=f; + while(t!=NULL) + { if(t->data==prev) + { temp->next=t->next; + t->next->pre=temp; + temp->pre=t; + t->next=temp; + fl=1; + break; + } + t=t->next; + } + if(fl==0) + printf("Node not found\n"); + } +} + +void del(int data) +{ struct node *t; + int fl=0; + if(f==NULL) + printf("Empty"); + else + { + if(f==e && f->data==data) + { t=f; + f=NULL; + e=NULL; + free(t); + } + else if(f->data==data) + { t=f; + f=f->next; + f->pre=NULL; + free(t); + } + else if(e->data==data) + { t=e; + e=e->pre; + e->next=NULL; + free(t); + } + else + { temp=f; + while(temp!=NULL) + { if(temp->data==data) + { t=temp; + temp->pre->next=temp->next; + temp->next->pre=temp->pre; + free(t); + fl=1; + break; + } + temp=temp->next; + } + if(fl==0) + printf("Node not found\n"); + } + } +} + +void display() +{ + if(f==NULL) + printf("Empty\n"); + else + { temp=f; + while(temp!=NULL) + { printf("%d\n",temp->data); + temp=temp->next; + } + } +} + +void main() +{ int i,data,ch,prev; + + while(1) + { printf("\n\nMENU \n1.Insertion at the begining \n2.Insertion at the end"); + printf("\n3.Insertion at the middle \n4.Deletion \n5.Exit"); + printf("\nEnter a choice:"); + scanf("%d",&ch); + if(ch==1) + { printf("Enter data:"); + scanf("%d",&data); + in_beg(data); + display(); + } + else if(ch==2) + { printf("Enter data:"); + scanf("%d",&data); + in_end(data); + display(); + } + else if(ch==3) + { printf("Enter data:"); + scanf("%d",&data); + printf("Enter Previous data:"); + scanf("%d",&prev); + in_mid(data,prev); + display(); + } + else if(ch==4) + { printf("Enter data to be deleted:"); + scanf("%d",&data); + del(data); + display(); + } + else if(ch==5) + break; + else + printf("Invalid choice"); + } +} + +