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"); + } +} 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"); + } +} + + diff --git a/README.md b/README.md index eb5fba2..86a4b0a 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,10 @@ 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 +- 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 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 + +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]); +} 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