views:

47

answers:

0

Hi I have written this code for having a non recursive Randomize-select algorithm !would you help me that is this correct also this is not my homework I want to make my ability to write such an algorithm.thanks

Algorithm Random-Select ( arr,1,n,i):
  If (n=1) then return arr[1]  
  randomPivot<-- Random-Partition(arr,1,n)
  k<--randomPivot
  if  (i = k)  then return arr[r]
  else   (i<k ) do
       booleanVariable<--true
       for m<-- 1 to r-1
           if booleanVariable is true
                Booleanvariable<--false
                min<--A[m]
                     for j<m+1 to r-1
                        if(arr[j] <min) then
                           min<--arr[j]
      Swap arr[m]<-> min
      If (m=i)
          return arr[m]
      else
          booleanVariable<--true

  else   (i>k ) do
       booleanVariable<--true
       for m<-- r+1 to n
           if booleanVariable is true
                Booleanvariable<--false
                min<--A[r+1]
                     for j<(r+1)+1 to n
                        if(arr[j] <min) then
                           min<--arr[j]
      Swap arr[m]<-> min
      If (m=i)
          return arr[m]
      else
          booleanVariable<--true