Why the second algorithm consume almost the same time as the first one?
Hi , everyone.
Could somebody explain why the second selection sort algorithm use much less time of copies but the time consumed is almost the same as the first one? I think it should be faster, is there something wrong with my way for testing?
Here is a testing result for sorting 10000 elements:
Algorithm 1:
Comparisons: 49995000 Copies: 146190
Time consumed: 1562
Algorithm 2
Comparisons: 49995000 Copies: 93071
Time consumed: 1578
Here is the complete Java program:
public class SelectionSort {
private static int selectionSortCompCounter = 0;
private static int selectionSortCopyCounter = 0;
// The first Selection Algorithm
static void selectionSort(Comparable[] a, int left, int right) {
// Sort the objects in a[left...right] into ascending order.
for (int l = left; l < right; l++) {
int p = l;
Comparable least = a[p];
for (int k = l + 1; k <= right; k++) {
int comp = a[k].compareTo(least);
// Increase comparions counter by 1.
selectionSortCompCounter++;
if (comp < 0) {
p = k;
least = a[p];
// Increase copies counter by 2;
selectionSortCopyCounter += 2;
}
}
if (p != l) {
a[p] = a[l];
a[l] = least;
// Increase copies counter by 2;
selectionSortCopyCounter += 2;
}
}
}
//The Second Selection Sort Algorithm
static void selectionSort2(Comparable[] a, int left, int right) {
for (int l = left; l < right; l++) {
int p = l;
for (int k = l + 1; k <= right; k++) {
int comp = a[k].compareTo(a[p]);
// Increase comparions counter by 1.
selectionSortCompCounter++;
if (comp < 0) {
p = k;
// Increase copies counter by 1;
selectionSortCopyCounter += 1;
}
}
if (p != l) {
Comparable temp = a[p];
a[p] = a[l];
a[l] = temp;
// Increase copies counter by 3;
selectionSortCopyCounter += 3;
}
}
}
static String random(int m) {
//Generate and return a random m-digit string.
char[] d = new char[m];
for (int i = 0; i < m; i++) {
d = (char) (10*Math.random() + '0');
}
return new String(d);
}
static String[] generateRandom(int n) {
String[] random = new String[n];
for (int i = 0; i < n; i++) {
random = random(3);
}
return random;
}
public static void main(String[] args) {
//For testing the first algorithm
String[] string = generateRandom(10000);
//For testing the second algorithm
String[] string2 = new String[10000];
// Makes string2[] contains exactly the same elements as string1[]
for(int i = 0; i < string.length; i++) {
string2 = string;
}
//Algorithm1 start time
long time1 = System.currentTimeMillis();
selectionSort(string, 0, 9999);
//Algorithm2 end time
long time2 = System.currentTimeMillis();
//Print the first algorithm result
System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);
System.out.println("Time consumed: " + (time2 - time1));
//Let counters be 0
selectionSortCompCounter = 0;
selectionSortCopyCounter = 0;
//Algorithm2 start time
long time3 = System.currentTimeMillis();
selectionSort2(string2, 0, 9999);
//Algorithm2 end time
long time4 = System.currentTimeMillis();
//Print the second algorithm result
System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);
System.out.println("Time consumed: " + (time4 - time3));
}
}
thx a lot ,guys :)
[3819 byte] By [
K_Ghosta] at [2007-10-2 9:50:13]

Your Solution !!!
public class SelectionSort {
private static int selectionSortCompCounter = 0;
private static int selectionSortCopyCounter = 0;
static void selectionSort(Comparable[] a, int left, int right) {
int iIncrement = 0;
int iAssignment = 0;
int iComparison = 0;
int iRetrieve = 0;
for (int l = left; l < right; l++) {
iIncrement++;
iComparison++;
int p = l;
iAssignment++;
Comparable least = a[p];
iAssignment++;
iRetrieve++;
for (int k = l + 1; k <= right; k++) {
iIncrement++;
iComparison++;
int comp = a[k].compareTo(least);
iAssignment++;
iComparison++;
iRetrieve++;
// Increase comparions counter by 1.
selectionSortCompCounter++;
iIncrement++;
if (comp < 0) {
iComparison++;
p = k;
iAssignment++;
least = a[p];
iAssignment++;
iRetrieve++;
// Increase copies counter by 2;
selectionSortCopyCounter += 2;
iAssignment++;
iIncrement++;
}
}
if (p != l) {
iComparison++;
a[p] = a[l];
iAssignment++;
iRetrieve++;
iRetrieve++;
a[l] = least;
iAssignment++;
iRetrieve++;
// Increase copies counter by 2;
selectionSortCopyCounter += 2;
iAssignment++;
iIncrement++;
}
}
System.out.println("Comparison : " + iComparison);
System.out.println("Assigment : " + iAssignment);
System.out.println("Increment : " + iIncrement);
System.out.println("Retrieval : " + iRetrieve);
}
//The Second Selection Sort Algorithm
static void selectionSort2(Comparable[] a, int left, int right) {
int iIncrement = 0;
int iAssignment = 0;
int iComparison = 0;
int iRetrieve = 0;
for (int l = left; l < right; l++) {
iIncrement++;
iComparison++;
int p = l;
iAssignment++;
for (int k = l + 1; k <= right; k++) {
iIncrement++;
iComparison++;
int comp = a[k].compareTo(a[p]);
iAssignment++;
iComparison++;
iRetrieve++;
iRetrieve++;
// Increase comparions counter by 1.
selectionSortCompCounter++;
iIncrement++;
if (comp < 0) {
iComparison++;
p = k;
iAssignment++;
selectionSortCopyCounter += 1;
iIncrement++;
iAssignment++;
}
}
if (p != l) {
iComparison++;
Comparable temp = a[p];
iAssignment++;
iRetrieve++;
a[p] = a[l];
iAssignment++;
iRetrieve++;
iRetrieve++;
a[l] = temp;
iAssignment++;
iRetrieve++;
selectionSortCopyCounter += 3;
iIncrement++;
iAssignment++;
}
}
System.out.println("Comparison : " + iComparison);
System.out.println("Assigment : " + iAssignment);
System.out.println("Increment : " + iIncrement);
System.out.println("Retrieval : " + iRetrieve);
}
static String random(int m) {
char[] d = new char[m];
for (int i = 0; i < m; i++) {
d = (char) (10 * Math.random() + '0');
}
return new String(d);
}
static String[] generateRandom(int n) {
String[] random = new String[n];
for (int i = 0; i < n; i++) {
random = "" + random(i);
}
return random;
}
public static void main(String[] args) {
final int RANGE = 10000;
String[] string = generateRandom(RANGE);
String[] string2 = new String[RANGE];
System.arraycopy(string, 0, string2, 0, string.length);
selectionSortCompCounter = 0;
selectionSortCopyCounter = 0;
long time1 = System.currentTimeMillis();
selectionSort(string, 0, RANGE - 1);
long time2 = System.currentTimeMillis();
System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);
System.out.println("Time consumed: " + (time2 - time1));
selectionSortCompCounter = 0;
selectionSortCopyCounter = 0;
long time3 = System.currentTimeMillis();
selectionSort2(string2, 0, RANGE - 1);
long time4 = System.currentTimeMillis();
System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);
System.out.println("Time consumed: " + (time4 - time3));
}
}
Algorithm 1::
Comparison : 100088296
Assigment : 50279889
Increment : 100088296
Retrieval : 50113276
49995000 176594
Time consumed: 7406
Algorithm 2::
Comparison : 100088296
Assigment : 50201573
Increment : 100088296
Retrieval : 100029960
49995000 108277
Time consumed: 7156
Please check the retrieval and comparison ....
Basic Assembly Instruction ...
Regards...