Hello World

Ive got a question. So basically ive got a program that asks you which file to open, and once u type in one, it dumps the contents on the screen. The contents im dealing with is a list of numbers.
So my question is, how can i get it to sort the numbers in ascending or descending order?
Ive thought about bubble sorting it, and using qsort, but as far as i can tell, both require the program to actually know the list of numbers, as opposed to looking one up in a file.

So what do i do?! Please i really need ur help, and would greatly appreciate it.

Thanks!

Insertion sort is easy to write and works well with a stream of items.

well here's what im dealing with, and i dont quite know where and how to use an insertion method, and everywhere i look online assumes the information is known in the form of an array or something else:

#include <stdio.h>

void main() 
{ 
	FILE *fopen(), *fp; 
	int c,d ; 
	char filename[40] ;

	printf("Enter the name of the file containing the signal data: ");
	gets( filename ) ;

	fp = fopen( filename, "r");  
 
	c = getc( fp ) ; 		 

	while (  c != EOF ) 
	{
		putchar(c); 		
		c = getc ( fp ); 	
	}

	fclose( fp ); 
	

}

i dont know wat to do! please help, thanks!

If you don't know how to use an array, I'd seriously question your ability to write a sorting function.

oh well thank you john. i guess you've answered my question now, so we can all go home.

If you say that you dont know the size of the file, then you can use a linked list. get a data using fscanf(), insert it into your list when you find the position. continue until end of file.

But if the file is very big, then use some external sort algorithm

This would prove to be very simple.

If you say that you dont know the size of the file, then you can use a linked list. get a data using fscanf(), insert it into your list when you find the position. continue until end of file.

But if the file is very big, then use some external sort algorithm

This would prove to be very simple.

hey ,
Use linked list as suggested by prabakar. Create a link list while reading file. Both these function will be in single loop. I mean your code should follow algorithm like,

  While(!End Of File){
          Read a number from file. 
          InsertIntoLinkList()
  }

  InsertIntoLinkList(){
          If link list is NULL, create node with number
          Else , traverse through list to find out insert position
  }

I think in this way u will achive sorting by in good time , bcaz in anyway , u have to read each number from file & using same interation to form link list will save ur time for Sorting will require in case of bubble sort. Anytime, your link list will be in sorted format.
Hope you got what I mean!

Regards
Sachin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.