patternjavaMinor
TurboSort challenge
Viewed 0 times
turbosortchallengestackoverflow
Problem
I recently started to do some of the exercises in CodeChef and I did the TurboSort challenge here is it's description. It works fine with smaller arrays but can't pass some of the tests due to the fact it's \$O(n^2)\$, so I need a faster way to sort them.
How can I improve it so it can run fast even with bigger arrays?
import java.util.Scanner;
public class TurboSort {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int arrSize = scan.nextInt();
int[] nums = new int[arrSize];
for(int i = 0; i nums[a]){
int swap = nums[j];
nums[j] = nums[a];
nums[a] = swap;
}
}
}
for(int p = 0; p < arrSize; p++){
System.out.println(nums[p]);
}
}
}How can I improve it so it can run fast even with bigger arrays?
Solution
Looking at your code, I'm not sure how to improve performance, but I think readability could be improved.
Naming things
Here are some variables you use:
Naming things
Here are some variables you use:
for(int i = 0; i < arrSize; i++ ){
// snip
for(int j = 0; j < arrSize; j++){
for(int a = j; a < arrSize; a++){
//snip
for(int p = 0; p < arrSize; p++){i, j, a and p carry no meaning. Perhaps it would be better to use names that describe what the variables represent. An example, adapt as needed:for(int arrayNumScan = 0; arrayNumScan < arrSize; arrayNumScan++ ){
// snip
for(int arrayIndex = 0; arrayIndex < arrSize; arrayIndex++){
for(int arrayIndexNum = arrayIndex; arrayIndexNum < arrSize; arrayIndexNum++){
// snip
for(int printArrayData = 0; printArrayData < arrSize; printArrayData++){Code Snippets
for(int i = 0; i < arrSize; i++ ){
// snip
for(int j = 0; j < arrSize; j++){
for(int a = j; a < arrSize; a++){
//snip
for(int p = 0; p < arrSize; p++){for(int arrayNumScan = 0; arrayNumScan < arrSize; arrayNumScan++ ){
// snip
for(int arrayIndex = 0; arrayIndex < arrSize; arrayIndex++){
for(int arrayIndexNum = arrayIndex; arrayIndexNum < arrSize; arrayIndexNum++){
// snip
for(int printArrayData = 0; printArrayData < arrSize; printArrayData++){Context
StackExchange Code Review Q#84689, answer score: 2
Revisions (0)
No revisions yet.