Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADDED insertion sort #192

Merged
merged 4 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Insertion_Sort/insertionsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C program for insertion sort
#include <math.h>
#include <stdio.h>

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}
222 changes: 222 additions & 0 deletions LinkedList/linked1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE CreateNode(NODE temp,int x);
NODE Init(NODE S);
NODE InsertL(NODE S,int x);
NODE InsertF(NODE S,int x);
NODE Insertsp(NODE S,int pos,int x);
NODE DeleteF(NODE S);
NODE Deletel(NODE S);
NODE Deletesp(NODE S,int pos);
void Display(NODE S);
int size(NODE S);
int main()
{
int ch,x;
int pos;
NODE S;
S=Init(S);
do
{
printf("------MENU------");
printf("\n 1.Initialization \n 2.Insert at beginning \n 3. Insert at last \n 4.Insert at specified position \n 5.Delete first \n 6.Delete last \n7.Delete sp \n8.display \n9.size \n10.exit ");
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: S=Init(S);
break;
case 2:printf("\n enter element to be inserted");
scanf("%d",&x);
S=InsertF(S,x);
break;
case 3: printf("\n enter element to be inserted");
scanf("%d",&x);
S=InsertL(S,x);
break;
case 4: printf("\n enter element to be inserted");
scanf("%d",&x);
printf("\n enter the position where u want to insert");
scanf("%d",&pos);
S=Insertsp(S,pos,x);
break;
case 5: S=DeleteF(S);
break;
case 6: S=Deletel(S);
break;
case 7:printf("\n enter the position where u want to delete");
scanf("%d",&pos);
S=Deletesp(S,pos);
break;
case 8: Display(S);
break;
case 9: size(S);
printf("\n size is %d",size(S));
break;
case 10: printf("\n exiting");
break;
default: printf("\n wrong choice");
}
}while(ch!=10);
return 0;
}

NODE CreateNode(NODE temp,int x)
{
temp=(NODE)malloc(sizeof(struct node));
temp->info=x;
temp->next=NULL;
return(temp);
}
NODE Init(NODE S)
{
S=NULL;
return(S);
}
NODE InsertF(NODE S,int x)
{
NODE temp;
temp=CreateNode(temp,x);
if(S==NULL)
S=temp;
else
{
temp->next=S;
S=temp;
}
return(S);
}
NODE InsertL(NODE S,int x)
{
NODE temp,t;
temp=CreateNode(temp,x);
if(S==NULL)
S=temp;
else
{
for(t=S;t->next!=NULL;t=t->next);
t->next=temp;
}
return(S);
}
NODE Insertsp(NODE S,int pos,int x)
{
int i;
int l=size(S);
if(pos<1||pos>(l+1))
{
printf("\n INSERTION NOT POSSIBLE");
return (S);
}
if(pos==1)
{
S=InsertF(S,x);
return(S);
}
if(pos==l+1)
{
S=InsertL(S,x);
return(S);
}
if(pos>1 && pos<=l)
{
NODE t,temp;
temp=CreateNode(temp,x);
for(t=S,i=1;i<(pos-1);i++,t=t->next);
temp->next=t->next;
t->next=temp;
}
}
void Display(NODE S)

{
if(S==NULL)
printf("\n NOTHING TO SHOW");
else
{
NODE t;
for(t=S;t!=NULL;t=t->next)
printf(" %d",t->info);
}
}
int size(NODE S)
{
NODE t;
int i=0;
for(t=S,i=0;t!=NULL;t=t->next,i++);
return(i);
}
NODE DeleteF(NODE S)
{
NODE t;
if(S==NULL)
printf("\n LIST IS EMPTY");
else
{
t=S;
S=S->next;
printf("\n DELETED NODE IS %d",t->info);
free(t);
}
return(S);
}
NODE Deletel(NODE S)
{
NODE t,p;
if(S==NULL)
printf("list is empty");
else
{
for(t=S;t->next!=NULL;p=t,t=t->next);
if(S->next==NULL)
S==NULL;
else
p->next=NULL;
printf(" deleteed info:%d",t->info);
free(t);
}
return(S);
}
NODE Deletesp(NODE S,int pos)
{
int i;
int l=size(S);
if(pos<1||pos>l)
{
printf(" not possible to delete");
return(S);
}
if(pos==1)
{
S=DeleteF(S);
return(S);
}
if(pos==l)
{
S=Deletel(S);
return(S);
}

if(pos>1&&pos<l)
{
NODE t,p;
int i;
for(t=S,i=1;i<pos;i++,p=t,t=t->next);
p->next=t->next;
printf(" deleted element is %d",t->info);
free(t);
}
return(S);
}






Loading