There are no's between 1 to 2000 in a file line by line.print the no's in sorted order in o(n) complexity

Showing Answers 1 - 8 of 8 Answers

siva

  • Sep 14th, 2006
 

Hi,

First copy the no's from file to a local array,coz it reduces time complexity...though space complexity increases.

Then perform a radix sort.. if u want even the worst case complexity to be O(n) else you have many sorting algorithms..

  Was this answer useful?  Yes

yael

  • Sep 18th, 2006
 

create an array of size 2000, and initialize its cells to 0.

then, for every number i in the file array[i+1] = 1;

after going over the file, just go over the array and print i for each array[i+1] that has 1.

  Was this answer useful?  Yes

manik sheeri

  • Nov 7th, 2006
 

the problem can be solved using one array of size 2000.copy all the numbers to the array at once and then use the following int array[2001];int tmp;for(int i=1;i<=2000;i++){tmp=array[i];array[i]=array[tmp];array[tmp]=tmp;}

  Was this answer useful?  Yes

vishal

  • Nov 30th, 2006
 

#include<stdio.h>

#include<conio.h>

void main()

{

 int i,j,temp;

 int num[10];

 clrscr();

 //printf("Enter the numbers please:n");

 /*.LOOP TO ENTER 10 NUMBERS.*/

// for(i=1;i<=2000;i++)

   // {printf("enter %d number:",i+1);

     //scanf("%d",&num[i]);

   // }

 /*.LOOP TO ARRANGE THE NUMBERS.*/

 for(i=1;i<=2000;i++)

    for(j=1;j<=2000;j++)

       if( num[j]>num[j+1])

       {

        temp=num[j];

        num[j]=num[j+1];

        num[j+1]=temp;

       }

 printf("The numbers in ascending order are:n");

 /*.LOOP TO PRINT THE NUMBERS....*/

 for(i=1;i<=2000;i++)

   printf("%dn",num[i]);

 getch( );

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions