Random generator and sorting

When i run my code a bunch of random numbers print out that shouldn't be there. Can someone please help me?

#include <stdio.h>
#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

void sectionSort(int a[], int n)
{
int i, j, min, temp;

for (int i = 0; i < n - 1; i++)
{
int minIndex = 0;

for (int j = i + 1; j < n; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
cout << a[i];
swap(a[minIndex], a[i]);
}
}


int main()
{
srand(time(0));
int i;
int n = 1001;
int a[1001];
printf("The original array is: \n");
for (i = 0; i < n; i++)
{
a[i] = rand() % n;
};
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\t", i, a[i]);
};
printf("\n");
printf("The sorted array is: \n");
sectionSort(a, n); //This is where it's printing the num.
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\t", i, a[i]);
};
return 0;
}
Your sort is effectively:
1
2
3
4
5
6
7
8
9
10
11
void sectionSort( int a[], int n )
{
  for (int i = 0; i < n - 1; i++)
  {
    auto next = std::min_element( a+1, a+n );
    auto minIndex = std::distance( a, next );

    cout << a[i];
    swap( a[minIndex], a[i] );
  }
}

Pay attention how you print the element before you have set its proper value.
Thank you for your help. It's greatly appreciated.
Topic archived. No new replies allowed.