sorting numbers in a file using arrays only

i wrote a code that sorts up to 10 numbers the user inputs and a negative number input ends the list. how would i sort a file that has at least 1000 numbers all positive? the sorting is from smallest to highest
this is the code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<fstream>
#include<cstdlib>
#include<iostream>
using namespace std;

void fill_array(int a[], int size, int&number_used);
void sort(int a[], int number_used);
void swap_values(double&v1, double&v2);
double index_of_smallest(const int a[], int start_index, int number_used);

int main()
{
	ifstream fin;
	ofstream fout;
	
	fin.open("numbers.dat");
	if (fin.fail())
	{
		cout<< "Input file opening failed.\n";
		exit(1);
	}

	fout.open("output.dat");
	if(fout.fail())
	{
		cout << "Output file opening failed.\n";
		exit(1);
	}
	
	fout<<"This program sorts numbers from lowest to highest.\n";
	
	int sample_array[2000], number_used;
	fill_array(sample_array, 2000, number_used);
	sort(sample_array, number_used);
	
	fout<<"The sorted numbers are:\n";
	for(int index=0; index<number_used; index++)
	fout<<sample_array[index]<<" ";
	cout<<endl;
	
	fin.close();
	fout.close();

	return 0;
}

void fill_array(int a[], int size, int&number_used)
{
     int next, index=0;
     fin>>next;
     while((next>=0)&&(index<size))
     {
                                   a[index]=next;
                                   index++;
                                   fin>>next;
     }
     number_used=index;
}

void sort(int a[], int number_used)
{
     double index_of_next_smallest;
     for(int index=0; index<number_used-1; index++)
     {
             index_of_next_smallest=index_of_smallest(a, index, number_used);
             swap_values(a[index], a[index_of_next_smallest]);
     }
}

void swap_values(double&v1, double&v2)
{
     double temp;
     temp=v1;
     v1=v2;
     v2=temp;
}

double index_of_smallest(const int a[], int start_index, int number_used)
{
       double min=a[start_index], index_of_min=start_index;
       for(int index=startindex+1; index<number_used; index++)
       if(a[index]<min)
       {
                       min=a[index];
                       index_of_min=index;
       }
       return index_of_min;
}

Hmm.. reposting isn't as effective as just continuing in your old post.
But, the answer is "the same way."

Here you have an inefficient bubble (my bad) selection sort, or some kind of variant of it I think, not really sure now that I look at it. Do the same thing with 1000 values, they should be sorted the same way.
Last edited on
Topic archived. No new replies allowed.