please help. I need to optimize my Bubble Sort algorithm in order to get less total comparisons than the non-optimised bubbleSort. I managed to create just the 'Normal Bubble sort (only travels from left to right):
def bubbleSort(values):
n = len(values) - 1
swap = True
ncomp = 0 # My total comparisons counter
while swap:
swap = False
for i in range(n): # i = 0, 1, 2, ..., n-1
ncomp += 1
if values[i] > values[i+1]:
temp = values[i]
values[i] = values[i+1]
values[i+1] = temp
swap = True
return values, ncomp
So basically i dont know how to create an 'optimised bubbleSort', a bubbleSortPlus function in which bubbles travel in both directions: from left to right, immediately followed by a travel from right to left. In theory, in each pass the travel of the bubbles should be shortened (saving in a variable the position of the last swap in a travel, and make the next travel start at that position. I tried so hard but i'm just a python newbie, please help.
Here's some skeleton code that shows how to scan the array forwards and backwards, while shrinking the list on each iteration.
values = 100,101,102,103,104,105
start = 0
stop = len(values)-1
while stop > start:
for i in range(start, stop):
print i, "compare", values[i], "with", values[i+1]
print "moved a large value to index", stop
print
stop = stop - 1
if stop == start:
break
for i in range(stop, start, -1):
print i, "compare", values[i], "with", values[i-1]
print "moved a small value to index", start
print
start = start + 1
I guess it has been optimized...
The naive Bubble sort does not include the swap flag. So it will not return until finish all O(n^2) comparisons in any cases. But with swap flag, the number of comparison will be almost linear if the input sequence has been "almost sorted".
Related
I am completing a problem where I have create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. For example: 12 --> 21, 513 --> 531, 12435 --> 12453, 9817121211 --> 9817122111.
I've recompiled my code over and over increasing performance but have eventually come unto a stop where I can't get it any faster. Does anyone have any advice? Its the itertools.permutations line which is taking the vast majority of the time.
def next_bigger(n):
num = str(n)
num1 = set(int(x) for x in str(num))
if num == num[0] *len(num):
return -1
#full_set = set(num)
lis = set(int(''.join(nums)) for nums in itertools.permutations(num, len(num)))
lis = sorted(lis)
try:
return int(lis[lis.index(n)+1])
except Exception:
return -1
Link to problem: https://www.codewars.com/kata/55983863da40caa2c900004e/train/python
If you are looking for better performance "time complexity wise", The approach would be to find the "key" of the algorithm. In this case you should ask yourself, what does it means to create the next bigger nummber? The answer is just as simple as a swap between two adjacent numbers. The code would be like this.
def next_bigger(n):
num_string = list(str(n))
for i in range(1, len(num_string)):
if i == len(num_string):
return -1
#find two the two numbers one bigger than the other with the minimun order
if num_string[-i] > num_string[-i-1]:
compare_reference = num_string[-i]
index_reference = -i
#check if the current number is smaller than any of the tail
for k, current in enumerate(num_string[-i:]):
if num_string[-i-1] < current and current < compare_reference:
compare_reference = current
index_reference = -i+k
#interchange the locations:
num_string[index_reference] = num_string[-i-1]
num_string[-i-1] = compare_reference
#check if the tail is larger than one digit
if i > 1:
#order the rest of the vector to create the smaller number (ordering it).
lower_part_ordered = sort_ascendant(num_string[-i:])
else:
lower_part_ordered = [num_string[-i]]
# create a string from the list
return int("".join(num_string[:-i] + lower_part_ordered))
# no match found means a number like 65311
return -1
While not a way to increase the permutations function performance per se, this was the method I found to increase performance of the code. many thanks to all that offered help!
def next_bigger(n):
num_string = list(str(n))
a = []
for i in range(1, len(num_string)):
if i == len(num_string):
return -1
p = int(num_string[-i])
q = int (num_string[-(i+1)])
if p > q:
a.append(num_string[:-(i+1)])
lis = list(num_string[-(i+1):])
if len(lis) > 1:
lis2 = list(set(lis))
lis2.sort()
qindex = lis2.index(str(q))
first = lis2[qindex+1]
a[0].append(first)
lis.remove(first)
lis.sort()
for j in range (len(lis)):
a[0].append(lis[j])
return int("".join(a[0]))
return -1
I am doing the Project Euler #67 in Python. My program, which worked for Project 18, does not work for Project 67.
Code (excludes the opening of the file and the processing of information):
for i in range(len(temp)):
list1 = temp[i]
try:
list2 = temp[i+1]
trynum1 = list1[lastinput] + max(list2[lastinput],list2[lastinput+1])
try:
trynum2 = list1[lastinput+1] + max(list2[lastinput+1],list2[lastinput+2])
if trynum1 > trynum2:
outputlist.append(list1[lastinput])
else:
outputlist.append(list1[lastinput+1])
lastinput += 1
except IndexError:
outputlist.append(list1[0])
except IndexError:
if list1[lastinput] > list1[lastinput+1]:
outputlist.append(list1[lastinput])
else:
outputlist.append(list1[lastinput+1])
Variables:
temp is the triangle of integers
outputlist is a list which stores the numbers chosen by the program
I know the answer is 7273, but my program finds 6542. I cannot find an error which causes the situation. Please may you help me on it.
Logic
My approach to this program is to find one number (list1[lastinput]) and add it up with the larger number of the two below it (trynum1), compare with the number to the right of the first number (list1[lastinput+1]), adding the larger number of two below it (trynum2). I append the larger one to the output list.
This approach is logically flawed. When you're in row 1, you don't have enough information to know whether moving right or left will lead you to the largest sum, not with only a 2-row lookahead. You would need to look all the way to the bottom to ensure getting the best path.
As others have suggested, start at the bottom and work up. Remember, you don't need the entire path, just the sum. At each node, add the amount of the better of the two available paths (that's the score you get in taking that node to the bottom). When you get back to the top, temp[0][0], that number should be your final answer.
I thought day and night about problem 18 and I solved it, the same way I solved this one.
P.S. 100_triangle.txt is without 1st string '59'.
# Maximum path sum II
import time
def e67():
start = time.time()
f=open("100_triangle.txt")
summ=[59]
for s in f:
slst=s.split()
lst=[int(item) for item in slst]
for i in range(len(lst)):
if i==0:
lst[i]+=summ[i]
elif i==len(lst)-1:
lst[i]+=summ[i-1]
elif (lst[i]+summ[i-1])>(lst[i]+summ[i]):
lst[i]+=summ[i-1]
else:
lst[i]+=summ[i]
summ=lst
end = time.time() - start
print("Runtime =", end)
f.close()
return max(summ)
print(e67()) #7273
Though starting from the bottom is more efficient, I wanted to see if I could implement Dijkstra's algorithm on this one; it works well and only takes a few seconds (didn't time it precisely):
from math import inf
f = open("p067_triangle.txt", "r")
tpyramid = f.read().splitlines()
f.close()
n = len(tpyramid)
pyramid = [[100 - int(tpyramid[i].split()[j]) for j in range(i+1)] for i in range(n)]
paths = [[inf for j in range(i+1)] for i in range(n)]
paths[0][0] = pyramid[0][0]
def mini_index(pyr):
m = inf
for i in range(n):
mr = min([i for i in pyr[i] if i >= 0]+[inf])
if mr < m:
m, a, b = mr, i, pyr[i].index(mr)
return m, a, b
counter = 0
omega = inf
while counter < n*(n+1)/2:
min_weight, i, j = mini_index(paths)
if i != n-1:
paths[i+1][j] = min( paths[i+1][j], min_weight + pyramid[i+1][j])
paths[i+1][j+1] = min( paths[i+1][j+1], min_weight + pyramid[i+1][j+1])
else:
omega = min(omega, min_weight)
paths[i][j] = -1
counter += 1
print(100*n - omega)
Here is my solution. Indeed you have to take the bottom - up approach.
Result confirmed with PE. Thanks!
def get_triangle(listLink):
triangle = [[int(number) for number in row.split()] for row in open(listLink)]
return triangle
listOfLists = get_triangle('D:\\Development\\triangle.txt')
for i in range(len(listOfLists) - 2, -1, -1):
for j in range(len(listOfLists[i])):
listOfLists[i][j] += max(listOfLists[i+1][j], listOfLists[i+1][j+1])
print(listOfLists[0][0])
I am trying to write code that is an insertion sort. I am trying to get the code to take 2 values and put them into a new list while sorting it. So far it just puts the values into the list without them being sorted, i'm not quite sure why
pos = 0
pos2 = 1
go = True
while go == True:
for i in range(len(ex)-1):
stack.append(ex[pos])
print(stack)
stack.append(ex[pos2])
print(stack)
if stack[pos] > stack[pos2]:
stack[pos], stack[pos2] = stack[pos2], stack[pos]
print(stack)
pos = pos + 2
pos2 = pos2 + 2
I know it's not efficient, but it is based off code i made for a bubble sort which does
go = True
add = 0
while go == True:
for i in range(len(ex)-1):
if ex[i] > ex[i+1]:
go = True
ex[i], ex[i+1] = ex[i+1], ex[i] #flips the numbers in the list
print(ex)
add = add + 1
if add >= len(ex):
go = False
EDIT
I have changed it drastically, but there is still a problem. It only swaps the values once, even if it needs to be swapped multiple times to be in the right place. Here is the code
pos = 0
while pos < len(ex)-1:
for i in range(len(ex)-1):
stack.append(ex[i])
print(stack)
if stack[i-1] > stack[i]:
stack[i-1], stack[i] = stack[i], stack[i-1]
pos = pos + 1
else:
pos = pos + 1
You have to compare ex[pos] with ex[pos2] then you append the right element first :
if ex[pos] > ex[pos2]:
stack[pos].append(ex[pos2])
else stack[pos].append(ex[pos])
print(stack)
Here is the pseudo code for a classic insertion sort from https://visualgo.net/sorting a great resource for learning sorting algorithms:
mark first element as sorted
for each unsorted element
'extract' the element
for i = lastSortedIndex to 0
if currentSortedElement > extractedElement
move sorted element to the right by 1
else: insert extracted element
And here is how you could implement insertion sort in python:
def insertion_sort(l):
for i in range(1, len(l)):
j = i-1
key = l[i]
while (l[j] > key) and (j >= 0):
l[j+1] = l[j]
j -= 1
l[j+1] = key
return l
Once you understand the basic insertion sort you should be able to understand where you went wrong in your implementation in that you are not properly storing stack[pos] in your implementation.
I have recently started learning programming, just completed a course on edX. I was trying to solve this problem on HackerRank and it is running out of time in each case. What am I doing wrong?
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
x = [int(x_temp) for x_temp in input().strip().split(' ')]
x.sort()
def transmitter(aList=[], target=0):
'''
accepts a list of house location, and a target location for the transmitter
returns the optimal number of transmitters required to cover all the houses
'''
List = aList[:]
start = target - k
end = target + k + 1
for i in range(start, end):
if i in List:
List.remove(i)
if not List:
return 1
m = max(List)
for e in List:
if transmitter(List, e) < m:
m = transmitter(List, e)
return 1 + m
m = max(x)
for e in x:
if transmitter(x, e) < m:
m = transmitter(x, e)
print(m)
I am pretty new to this. Sorry for making any obvious mistakes, or for posting this here in case this is not the suitable site. In that case, it will be really helpful if you can recommend a site where I can ask such question.
the screenshot of the question
I'm pretty sure a greedy algorithm solves this problem optimally in just O(N) time. There's not need for any recursion. Just place each transmitter in turn as far to the right as you can without leaving any houses to its left uncovered. Stop when the last house is covered.
Here's how I'd code that:
def hackerland(houses, k): # houses should be sorted list of locations
first = None # location of first uncovered house
last = 0 # last location covered by a previous transmitter
prev = None
count = 0 # transmitters = []
for x in houses:
if first is not None and x > first + k:
first = None
count += 1 # transmitters.append(prev)
last = prev + k
if last is not None and x > last:
last = None
first = x
prev = x
if first is not None:
count += 1 # transmitters.append(prev)
return count # return transmitters
I've included comments that show how this code could be easily modified to return a list of the transmitter locations, rather than just a count of how many are needed.
It is not necessary to take a recursive approach. In fact, you can just work forward, iterate over the houses, placing transmitters when the previously placed one does not reach far enough to cover the current house, etc.
It is a bit more complicated than that, but not much. See this code:
# input
n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
x = [int(x_temp) for x_temp in input().strip().split(' ')]
# eliminate duplicate house x-xoordinates, they don't influence the result
houses = list(set(x))
houses.sort()
# add extreme far dummy house (will make the loop easier)
houses.append(100000)
reachedX = 0 # coordinate until where the previously placed transmitter reaches
unreachedX = -1 # coordinate that the next one needs to cover (to the left)
lastHouseId = -1 # index where previous transmitter was placed
transmitters = [] # coordinates of the placed transmitters
for houseId, houseX in enumerate(houses):
if reachedX > unreachedX: # we might still be in range of last transmitter
if houseX > reachedX: # we just went out of reach
unreachedX = houseX # this house must be covered by next one
elif houseX - k > unreachedX: # transmitter here wouldn't reach far enough back
lastHouseId = houseId - 1 # place it on previous house
reachedX = houses[lastHouseId] + k
transmitters.append(houses[lastHouseId])
print(transmitters)
print(len(transmitters))
I have been trying to implement the quicksort function using python for the last three weeks but I always get to this point and it sorts mostly, but there are a few items out of place.
I don't think I understand the quicksort properly, so if you can see why from my code please explain.
I have chosen the pivot to be the first object in the list, "low", and then comparing it to the rest of the list. If the object at list index "low" is greater than list index "i" (in my for loop), then I switch "i" with "E" (initially indexed to the item "low + 1"), if it does switch, "E" increments. Even if it doesn't switch, "i" increments (because of the for loop).
Once my loop is finished, I decrement "E" (to index it to the highest number in the list lower than my pivot) then switch it with "low" (index of pivot)
I then quicksort the left and right halves of the list using "E" to determine where the list splits. - this seems to be the point where the code fails to sort.
I believe this is how the quicksort works, but I haven't been able to make it work. If you know what I'm missing or if it's just one of my lines, please let me know. Any help with this problem would be greatly appreciated.
(PS. The "main" function is just passing a list of 20 length with variables of 0-19 value into my quicksort and the Python build-in sort)
import random
def quick(A, low, high):
if high <= low:
return
elif high > low:
E = low+1
for i in range(E, high):
if A[low] > A[i]:
A[i], A[E] = A[E], A[i]
E +=1
E -= 1
A[low], A[E] = A[E], A[low]
quick(A, low, E-1)
quick(A, E+1, high)
def main():
listA = []
listB = []
for i in range(20):
int = random.randrange(0, 19)
listA.append(int)
for i in range(len(listA)):
listB.append(listA[i])
print("List A (before sort)" + str(listA))
print("List B (before sort)" + str(listB))
quick(listA, 0, len(listA)-1)
print("\nList A (after sort)" + str(listA))
print("List B (before sort)" + str(listB))
listB.sort()
print("\nList A (after sort)" + str(listA))
print("List B (after sort)" + str(listB))
main()
Your problem is that you're ignoring one number with each split. range(min, max) gives a list that includes min but not max, ending rather on max-1
quick(listA, 0, len(listA)-1)
should be
quick(listA, 0, len(listA)),
and
quick(A, low, E-1)
should be
quick(A, low, E).