Solved

# Urgent Program require

Posted on 2000-02-19
212 Views
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]);
}

0
Question by:priyapinto
• 5
• 4
• 4
• +4

LVL 3

Expert Comment

ID: 2538485
Well, for one, you might want to format it so that we can actually read it.
0

LVL 84

Expert Comment

ID: 2538868
#include <stdlib.h>
void qsort (void* base, size_t nel, size_t width, int (*compar)(const void *, const void *));
0

LVL 1

Expert Comment

ID: 2538986
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);

0

LVL 1

Expert Comment

ID: 2539021
#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);
}
0

LVL 1

Expert Comment

ID: 2539022
Raise the points!
0

Author Comment

ID: 2539186
0

Author Comment

ID: 2539187
There are 2 programs that are needed. and kindly finish hem completely and I'll raise the points
0

LVL 1

Expert Comment

ID: 2541115
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.
0

Expert Comment

ID: 2550115
the program u have given is ok as far as insertion sort is oncerned. what do u need in that?
0

Expert Comment

ID: 2550299
0

LVL 24

Expert Comment

ID: 2555475
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.
0

LVL 24

Expert Comment

ID: 2606387
0

Author Comment

ID: 2606735
The question was not answered correctly and completely. And it is no longer urgent
0

Author Comment

ID: 2606736
This question has a deletion request Pending
0

LVL 24

Expert Comment

ID: 2606818
This question no longer is pending deletion
0

LVL 24

Expert Comment

ID: 2606819
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.
0

Accepted Solution

euy earned 30 total points
ID: 2619030
Hi there priyapinto. I have a code here which takes a user input of numbers and sorts out these numbers. Insertion sort in other words. All you have to do is to compile this program and run to see what it does. I tried debugging your program and i found out that in cases where the number of inputs is just below ten, it works fine. But if the number of inputs are ten or greater, it doesnm't work well. So i think uyou better try this one. As of now it can accomodate only 20 inputs but you can increase it in the future if you want.
About Merge Sort, I am not familiar with the word, but if i get it right, it's almost the same as Insertion Sort, so you should be able to modify this code of mine to become a Merge Sort program huh,..Okay,...good luck and happy studying. I hope you will learn something here, especially the sorting part.
By the way, if you need to sort it in descending order, and you have some difficulties, i have the code ready for that too. Here is the code,...bye,...

INSERT_SORT.C
-------------

#include <stdio.h>

int store_values(int z,int Num_arr[20]);
void sort_ascend(int numofele,int Num_arr[20]);
void disp_values(int ctr,int Num_arr[20]);

void main()
{
int Num_arr[20];
int st, i;

st=0;
/*array initialization*/
for(i=0; i<20; ++i)
Num_arr[i]=0;

st=store_values(st,Num_arr);
sort_ascend(st,Num_arr);
disp_values((st-1),Num_arr);
}

/*function for storing values*/
int store_values(int z,int Num_arr[20])
{
int y,sign;

printf("\n\nInput -999 if you want to exit the process.");
for(y=z; y<20; ++y)
{
printf("\n\n\t\tValue for array[%d]:",y);
scanf("\n%d",&sign);
if(sign!=-999)
Num_arr[y]=sign;
else
return y;
}
printf("\n\n\tARRAY IS FULL!!!\n\n");
return y;
}

/*sorting in ascending order*/
void sort_ascend(int numofele,int Num_arr[20])
{
int x,n,storage;

n=0;
for(x=0; x<numofele; x++)
{
n++;
while(n<numofele)
{
if(Num_arr[x]<Num_arr[n])
n=n+1;
else
{
storage=Num_arr[x];
Num_arr[x]=Num_arr[n];
Num_arr[n]=storage;
n=n+1;
}
}
n=x+1;
}
}

/*displaying the current values of the array in ascending order*/
void disp_values(int ctr,int Num_arr[20])
{
int x;

printf("\n\n\n   The array has %d elements!",ctr+1);
printf("\n   ARRAY = ");
for(x=0; x<=ctr; ++x)
printf(" %d ",Num_arr[x]);
printf("\n\n");
}

0

Author Comment

ID: 2621929
Thanks
0

## Featured Post

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.