Quote
Given a list of numbers, you are to sort them into descending order.
Input
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
Output
Output given numbers in non decreasing order.
Example
Input:
5
5
3
6
7
1
Output:
1
3
5
6
7
Input
t – the number of numbers in list, then t lines follow [t <= 10^6].
Each line contains one integer: N [0 <= N <= 10^6]
Output
Output given numbers in non decreasing order.
Example
Input:
5
5
3
6
7
1
Output:
1
3
5
6
7
The major problem I am running into is the built-in functions in Java and C++ are taking way too long. I figure if the built-in functions in the JDK and STL are not able to handle it, then if I write my own sorting algorithm that won't work also. Then again maybe quick sort and merge sort are not the right sorts, and I should be using my own quicksort with insertion sort of something to speed it up?
I'm obviously missing something because these codes are not working fast enough:
class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
ArrayList<Integer> aloNums = new ArrayList<Integer>();
int nNums = sin.nextInt();
for (int i=0;i<nNums;i++) {
aloNums.add(sin.nextInt());
}
Collections.sort(aloNums);
for (int i=0;i<aloNums.size();i++) {
System.out.println(aloNums.get(i));
}
}
}
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
vector<int> v;
int nNum;
int n;
cin >> nNum;
for (int i=0;i<nNum;i++) {
cin>>n;
v.push_back(n);
}
sort(v.begin(),v.end());
for (int i=0;i<nNum;i++) {
cout << v[i] << endl;
}
return 0;
}
It is going to have to use some kind of list, or vector as an array is not big enough, I tried. Thoughts?


Sign In
Create Account


Back to top










