Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
// Title:    Merge short
// Author:   Chandru K.S
// Date :    20-10-11

#include<stdio.h>
#include<conio.h>

struct node
{
int data;
struct node *next;
}*start = NULL;

int len;

void main()
{
struct node *start1;
clrscr();
insert(5);
insert(2);
insert(4);
insert(6);
insert(1);
//insert(3);
len = display();
printf("\nlist length is %d\n",len);

start1 = mergesort();
display1(start1);

getch();
}

insert(int val)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->next = NULL;
if(start==NULL)
{
start = temp;
}
else
{
struct node *last = start;

while(last->next!= NULL)
last = last->next;

last->next = temp;
}
return 0;
}

display()
{
int len=0;
struct node *temp = start;
while(temp)
{
printf("%d\t",temp->data);
len=len++;
temp = temp->next;
}
return len;
}

display1(struct node *temp)
{
int len=0;
//struct node *temp = start;
while(temp)
{
printf("%d\t",temp->data);
len=len++;
temp = temp->next;
}
return len;
}
mergesort()
{
struct node *head = start;
struct node *head_one;
struct node *head_two;
struct node *head_three;

head_one = head;
head_two = head->next;
while((head_two != NULL) && (head_two->next != NULL))
{
head = head->next;
head_two = head->next->next;
}
head_two = head->next;
head->next = NULL;

//merge_sort(head_one, head_two);


//struct node *merge_sort(struct node *head_one, struct node *head_two)

if(head_one == NULL)
return head_two;

if(head_two == NULL)
return head_one;

if(head_one->data < head_two->data)
 {
head_three = head_one;
head_three->next = merge_sort(head_one->next, head_two);
}
else
{
head_three = head_two;
head_three->next = merge_sort(head_one, head_two->next);
}

//return head_three;
}

/*   merge_sort(len)
{
int i;
int j;
int k;
int q; //middle node
//int p; //starting node
//int r; // last node

struct node *first = start;
struct node *second;
struct node *l = NULL; // first list
struct node *r = NULL; // second list
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));

q = floor((len)/2);

for(i=0;i<q;i++)
{
l = inser(first);
dis(l);
for(j=q;j<len;j++)
{
r = inser(first);
dis(r);
}
}
}  */
Posted

1 solution

Read the following :
Merge sort in C[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900