Read File and Selection Sort
Apr 28, 2018 at 12:15pm UTC
Hi, I need to read a file with a name data.txt instead of array in this code. This is selection sort. Can you tell me if its gonna sort 100,000 elements fine?
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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int arr[] = {57,654,465,456,78,4,8,1,6,0};
for (int i=0; i<10; i++)
{
cout<<arr[i]<< ',' ;
}
cout<<endl<<endl;
for (int i = 0; i< 10; i++)
{
int smallest = arr[i];
int smallestIndex = i;
for (int m=i; m<10; m++)
{
if (arr[m]<smallest)
{
smallest=arr[m];
smallestIndex=m;
}
}
swap(arr[i], arr[smallestIndex]);
}
for (int i = 0; i<10; i++)
{
cout<<arr[i]<< ',' ;
}
cout<<endl<<endl;
return 0;
}
Apr 28, 2018 at 3:08pm UTC
O(n^2) is going to take a long time for 100e3 elements
however, for (int i = 0; i< 10; i++)
would only consider 10.
Topic archived. No new replies allowed.