Link to home
Start Free TrialLog in
Avatar of priyapinto
priyapinto

asked on

Urgent Program require

Could you give me the program for Insertion sort and Merge Sort??? It is urgent.
the following is the program for insertion sort that I'd tried out please debug it.

#include<stdio.h>
void main()
{
int x[20],n,i=0,y,j=0;
clrscr();
printf("The no of numbers in the sorted list");
scanf("%d",&n);
//x[]=x[n];
for(i=0;i<n;i++)
{
printf("Enter a number");
scanf("%d",&x[i]);
}
for(i=1;i<n;i++)
{
y=x[i];
for(j=i-1;j>=0;j--)
{
if(x[i]<x[j])
{
//x[j+1]=x[j];
//x[j+1]=y;
x[i]=x[j];
x[j]=y;
}
//break;
}
}
for(i=0;i<=n;i++)
printf("\n%d",x[i]);
}

Avatar of packratt_jk
packratt_jk
Flag of United States of America image

Well, for one, you might want to format it so that we can actually read it.
Avatar of ozo
#include <stdlib.h>
void qsort (void* base, size_t nel, size_t width, int (*compar)(const void *, const void *));
You are stomping memory. What happens if i  enter 50 after the 1st prompt?  There is only enough memory to hold 20 ints.

int *x,n,i=0,y,j=0;
printf("The no of numbers in the sorted list");
scanf("%d",&n);
x = malloc(sizeof (int) * n);
....
free (x);


#include <stdio.h>
#include <stdlib.h>

void main()
{
int *x,n,i=0,y,j=0;
printf("The no of numbers in the sorted list\n");
scanf("%d",&n);
x = malloc(sizeof (int) * n);

for (i = 1; i <= n; i++) {
  printf("Enter a number\n");
  scanf("%d",&x[i]);
}

for (i = 2; i <= n; i++) {
  y = x[i];
  j = i - 1;
  while((j > 0) && (x[j] > y)) {
    x[j + 1] = x[j];
    j -= 1;
  }
  x[j + 1] = y;
}

for (i = 1; i <= n; i++)
  printf("%d\n",x[i]);
// still working on this part
//free(x);
}
Raise the points!
Avatar of priyapinto
priyapinto

ASKER

Adjusted points to 30
There are 2 programs that are needed. and kindly finish hem completely and I'll raise the points
We are not allowed to do homework problems here.  It is grounds for removal for the expert as well as the person asking the question.  You post what you have for the other one and I'll help.
the program u have given is ok as far as insertion sort is oncerned. what do u need in that?
Ya, I get confused on bounds, too, when to stop and finish it with integrity. Give or take one.  My personal preference is that the First item used should be numbered as = 1. Where possible.
Is it still urgent? or did comments above help you to fix it up
The question was not answered correctly and completely. And it is no longer urgent
This question has a deletion request Pending
This question no longer is pending deletion
This is not meant to only be a vehicle to do all of your work for you perfectly. It is meant to be another aid that can help you do work yourself both now and later in life with what you have learned.

You have an opportunity to award points with a value (or grading) of less than perfect. Before deleting, reconsider than others like snifong and vissteve and others have given to you here freely of their time.

A 'correct' solution may be in Eye of Beholder, while

To achieve a more 'complete' solution it behooves the person posing the question to be responsive to contributions, saying what is better or not for needs. To indicate a point in progress so that others can key in on the parts that are missing, if any.

I ask you, did you give 'complete' feedback to those trying to assist you? Did you 'correct' any misunderstandings of the problems you faced?

If so, go ahead and Delete on these potential helpers.
ASKER CERTIFIED SOLUTION
Avatar of euy
euy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks