Help With Costructing Selection sort?

Using a selection sort, for each entry in the array, display the original index of the first dimension, the second dimension's value, and the first dimension's value on a single line separated by : (colon) sorted by the first dimension.

I got the two dimensional array figure out just dont know how to incorporate the above question.

Thanks in advance!

View Answers

July 12, 2012 at 11:56 AM

Please visit the following link:

Java Selection Sort


July 14, 2012 at 10:48 PM

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class SelectionSort {

 /**
  * @Author [Chandrasekhara Kota][1]
  */
 public static void main(String[] args) {
  int arr[]={9,1,8,5,7,-1,6,0,2,2718};
  int sortedArr[]=selectionSort(arr);
  for (int i = 0; i <sortedArr.length; i++) 
  { 
   System.out.println(sortedArr[i]);
  }

 }

 private static int[] selectionSort(int[] arr) { 

  int  minIndex, tmp; 
  int n = arr.length; 
  for (int i = 0; i < n - 1; i++) 
  { 
              minIndex = i; 
              for (int j = i + 1; j < n; j++) 
                   if (arr[j] < arr[minIndex]) 
                        minIndex = j; 
              if (minIndex != i) { 
                    tmp = arr[i]; 
                    arr[i] = arr[minIndex]; 
                    arr[minIndex] = tmp; 
              } 
        }
  return arr; 
 }
}









Related Tutorials/Questions & Answers:
Advertisements