Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

This code sort the string below in ascending order.

I'd prefer it split-up in two or three smaller, simpler methods. I'm also wondering whether my algorithm has a decent time complexity.

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}

This code sort the string below in ascending order.

I'd prefer it split-up in two or three smaller, simpler methods. I'm also wondering whether my algorithm has a decent time complexity.

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}

This code sort the string below in ascending order.

I'd prefer it split-up in two or three smaller, simpler methods. I'm also wondering whether my algorithm has a decent time complexity.

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}
deleted 10 characters in body
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

This code sort the string below in ascending order.

Q1: IsI'd prefer it possible to split this code into three or-up in two or three smaller, simpler methods?

Q2: How to determine the. I'm also wondering whether my algorithm has a decent time complexity for this algorithm?.

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}

This code sort the string below in ascending order.

Q1: Is it possible to split this code into three or two smaller simpler methods?

Q2: How to determine the time complexity for this algorithm?

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}

This code sort the string below in ascending order.

I'd prefer it split-up in two or three smaller, simpler methods. I'm also wondering whether my algorithm has a decent time complexity.

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}
Source Link
Adam
  • 405
  • 1
  • 7
  • 15

Sort a given string in ascending order

This code sort the string below in ascending order.

Q1: Is it possible to split this code into three or two smaller simpler methods?

Q2: How to determine the time complexity for this algorithm?

Given string

[H, B, D, G, F, E, A, C]

Output

[A, B, C, D, E, F, G, H]

public class sortArray {
 public static void sort (String[] str)
 {
 int lastPos = str.length - 1;
 int minPos = 0;
 String s = "";
 for (int i = 0; i < lastPos; i++)
 {
 minPos = i;
 for (int j = i + 1; j <= lastPos; j++)
 if (str[j].compareTo (str[minPos]) < 0)
 minPos = j;
 if (minPos != i)
 {
 s = str[i];
 str[i] = str[minPos];
 str[minPos] = s;
 }
 }
 }
 public static void main(String[] args){
 String[] str = {"H", "B", "D", "G","F", "E", "A", "C"};
 sort(str);
 System.out.println(Arrays.toString(str));
 }
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /