Groovy web console

subscribe to the feed Subscribe
to this
site

SelectionSort2_2

Published 2 months ago by Carsten Schulz
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def selectionSort(A) {  
 for (i = 0; i < A.size(); i+=2) 
 { 
 min = i
 min2 =i+1
 
 for (j = i+1; j < A.size(); j++) 
 { 
 if (A[j] < A[min]) min = j
 if (A[j] < A[min2]&&A[j]!=A[min]) min2 = j
 }

 temp = A[i] 
 A[i] = A[min] 
 A[min] = temp 
  
 temp = A[i+1] 
 A[i+1] = A[min2] 
 A[min2] = temp 

 } 
 
 return A 
 }
 
 L = [1, 5, 99, 66, 4, 7]
 println selectionSort(L)