Created
November 12, 2019 15:02
-
-
Save abdellani/43503be4abf0e8e299e369137adcbaf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def asq(array,start,last_element_index) | |
counter=0 | |
return counter if start>=last_element_index | |
left=start | |
while left <last_element_index | |
if array[left]> array[last_element_index] | |
i=left+1 | |
exachange=false | |
while i<last_element_index | |
if array[i]< array[last_element_index] | |
tmp = array[i] | |
array[i]=array[left] | |
array[left]=tmp | |
exachange=true | |
break | |
end | |
i+=1 | |
end | |
break if !exachange | |
end | |
left+=1 | |
end | |
counter =left -start+1 | |
tmp=array[left] | |
array[left]=array[last_element_index] | |
array[last_element_index]=tmp | |
counter+=asq(array,start,left-1) | |
counter+=asq(array,left+1,last_element_index) | |
counter | |
end | |
def sort_itself(array) | |
# write your code here | |
counter=0 | |
i=0 | |
while i<array.length-1 | |
j=i | |
while j>=0 && array[j]>array[j+1] | |
counter+=1 | |
tmp = array[j] | |
array[j]=array[j+1] | |
array[j+1]=tmp | |
j-=1 | |
end | |
i+=1 | |
end | |
counter | |
end | |
def advanced_quicksort(array) | |
# write your code here | |
arr=array.dup | |
sort_itself(array)-asq(arr,0,array.length-1) | |
end | |
def quicksort_running_time(array) | |
# write your code here | |
advanced_quicksort(array) | |
end | |
puts quicksort_running_time([1, 3, 9, 8, 2, 7, 5]) | |
# => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment