Solved

Urgent Program require

Posted on 2000-02-19
18
232 Views
Last Modified: 2010-04-15
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
Comment
Question by:priyapinto
  • 5
  • 4
  • 4
  • +4
18 Comments
 
LVL 3

Expert Comment

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

Expert Comment

by:ozo
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

by:snifong
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Expert Comment

by:snifong
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

by:snifong
ID: 2539022
Raise the points!
0
 

Author Comment

by:priyapinto
ID: 2539186
Adjusted points to 30
0
 

Author Comment

by:priyapinto
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

by:snifong
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

by:vissteve
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

by:vissteve
ID: 2550299
0
 
LVL 24

Expert Comment

by:SunBow
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

by:SunBow
ID: 2606387
Is it still urgent? or did comments above help you to fix it up
0
 

Author Comment

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

Author Comment

by:priyapinto
ID: 2606736
This question has a deletion request Pending
0
 
LVL 24

Expert Comment

by:SunBow
ID: 2606818
This question no longer is pending deletion
0
 
LVL 24

Expert Comment

by:SunBow
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

by:
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

by:priyapinto
ID: 2621929
Thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now