[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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]);
}

0
priyapinto
Asked:
priyapinto
  • 5
  • 4
  • 4
  • +4
1 Solution
 
packratt_jkCommented:
Well, for one, you might want to format it so that we can actually read it.
0
 
ozoCommented:
#include <stdlib.h>
void qsort (void* base, size_t nel, size_t width, int (*compar)(const void *, const void *));
0
 
snifongCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
snifongCommented:
#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
 
snifongCommented:
Raise the points!
0
 
priyapintoAuthor Commented:
Adjusted points to 30
0
 
priyapintoAuthor Commented:
There are 2 programs that are needed. and kindly finish hem completely and I'll raise the points
0
 
snifongCommented:
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
 
vissteveCommented:
the program u have given is ok as far as insertion sort is oncerned. what do u need in that?
0
 
vissteveCommented:
0
 
SunBowCommented:
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
 
SunBowCommented:
Is it still urgent? or did comments above help you to fix it up
0
 
priyapintoAuthor Commented:
The question was not answered correctly and completely. And it is no longer urgent
0
 
priyapintoAuthor Commented:
This question has a deletion request Pending
0
 
SunBowCommented:
This question no longer is pending deletion
0
 
SunBowCommented:
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
 
euyCommented:
  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
 
priyapintoAuthor Commented:
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now