Will take O(n * log (n)) time, and does not depend on input.
Java
C#
Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts
Wednesday, July 27, 2011
Tuesday, July 26, 2011
Insertion sort
Efficient for small lists, with size less then 30.
Special cases are:
Java
C#
Special cases are:
- if list is almost sorted, it will be near linear time
- if sorted list is reversed, it will be quadratic time
Java
C#
Tuesday, September 8, 2009
Euler Problem 22 solution
Time (s): ~0.096
package margusmartseppcode.From_20_to_29;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Problem_22 {
public static void main(String[] args) throws FileNotFoundException {
String raw = new Scanner(new File("names.txt")).next();
String str[] = raw.substring(1, raw.length() - 1).split("\",\"");
long ls = 0, ts = 0;
Arrays.sort(str);
for (int i = 0; i < str.length; i++, ls = 0) {
for (char c : str[i].toCharArray())
ls += c - '@';
ts += ls * (i + 1);
}
System.out.println(ts);
}
}
Labels:
Euler Problem 20-29,
File,
Scanner,
sorting,
Strings
Subscribe to:
Posts (Atom)