Last active
December 28, 2015 13:39
-
-
Save prat0318/7509212 to your computer and use it in GitHub Desktop.
next greater in array
This file contains hidden or 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 next_greater(arr): | |
while(not is_rev_sorted(arr)): | |
for i in reversed(range(len(arr) - 1)): | |
if arr[i] < arr[i+1]: break | |
for j in reversed(range(len(arr))): | |
if arr[i] < arr[j]: break | |
swap(arr, i, j) | |
yield reverse(arr, i+1) | |
def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] | |
def reverse(arr, i): | |
j = len(arr) - 1 | |
while( i < j): | |
swap(arr, i, j) | |
i+=1; j-=1 | |
return arr | |
def is_rev_sorted(arr): | |
rev=True | |
for i in range(len(arr) - 1): | |
if arr[i] < arr[i+1]: | |
rev=False | |
break | |
return rev | |
a = [1,2,3,4] | |
for i in next_greater(a): | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment