Sorting using pointers

My program reads the numbers from input file and writes the numbers sorted to output file.

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
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>

using namespace std;


int main()
{
	int A[100];
	//Reading Numbers From Input File
	ifstream in("input.txt");

	int i=0;
	while(in >> A[i])
	{
		i++;
	}
	int n=i;
	in.close();

	//Using Select Sort
	for(i = 0; i<= n-1; i++)
        for(int j=i+1; j <= n; j++)
            if(A[i] > A[j])
            {
                int temp = A[i];
                A[i] = A[j];
                A[j] = temp;
            }  
	
	// Writing to output file
	ofstream out("output.txt");
		for(i=1; i<=n; i++)
			out << A[i] <<" ";
		out.close();
		return 0;
}


My program works fine, but I need to optimize it.
I need a sorting algorithm that uses pointers and not arrays.
Last edited on
Then you could do something like: int* A=new int[100]; this will be a pointer to an array on the heap. And don't forget the delete aspect of it.
Better still, you could use a vector of integers.

Aceix.
Last edited on
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
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>

using namespace std;

unsigned int PRNG()
{
    // our initial starting seed is 5323
    static unsigned int nSeed = 5323;
 
    // Take the current seed and generate a new value from it
    // Due to our use of large constants and overflow, it would be
    // very hard for someone to predict what the next number is
    // going to be from the previous one.
    nSeed = (8253729 * nSeed + 2396403);
 
    // Take the seed and return a value between 0 and 100
    return nSeed  % 100;
}

int main()
{
	int *A = new int[100]; // pointer to an array on the heap
	int i;

	//Writing Random Numbers to input file
	ofstream randomNumbers("input.txt");
	  for(i=1; i<100; i++){
		randomNumbers << PRNG() <<" ";
	  }
	//Reading Numbers From Input File
	ifstream in("input.txt");

	i=0;
	while(in >> *(A+i))   // reading all elements 
	{
		i++;
	}
	int n=i;  // number of numbers
	in.close();

	//Sorting the numbers
	for(i = 0; i<= n-1; i++)
        for(int j=i+1; j <= n; j++)
            if(*(A+i) > *(A+j))
            {
                int temp = *(A+i);
                *(A+i) = *(A+j);
                *(A+j) = temp;
            }  
	
	// Writing to output file
	ofstream out("output.txt");
		for(i=1; i<=n; i++)
			out << *(A+i) <<" ";
		out.close();
		
		delete[] A; // delete the memory allocated on the heap 

		return 0;
}


I have updated my code using pointers for sorting, and writing random numbers in the input file.
But I have one small problem, now it doesn't write anything in my output file after sorting.
Sorry for the question:
I forgot to close randomNumbers randomNumbers.close();

Now it works.

So what are your opinions on the sorting efficiency I made using pointers?

Is is better or worse? Is there any better way?
Last edited on
There is no difference in your algorithms. If A is a pointer to int then *(A+i) is identical to A[i].

You would gain far more by using a faster sort algorithm.
Please... please prefer A[i] over *(A+i).

Please. My heart dies a little inside whenever I see *(A+i)
Last edited on
Topic archived. No new replies allowed.