Sort a list of number with sort function and without sort function. Explain it with a dry run of the code
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 8 )
unsorted_list = [2,3,1,4,5]
for i in range (0,len(unsorted_list)):
for j in range (i+1,len(unsorted_list)):
if unsorted_list[i]<unsorted_list[j]:
unsorted_list[i], unsorted_list[j]= unsorted_list[j],unsorted_list[i]
print(unsorted_list)
Using the Sort function:
unsorted_list=[2,3,1,4,5]
unsorted_list=[2,3,1,4,5]
Output:
[5, 4, 3, 2, 1]
Without using the sort function:
unsorted_list=[2,3,1,4,5]
x=len(unsorted_list)
sorted_list=[]
i=1
while i<=x:
sorted_list.append(max(unsorted_list))
unsorted_list.remove(max(unsorted_list))
i=i+1
print(sorted_list)
output:
[5, 4, 3, 2, 1]
Forgot to explain my code.
Here goes:
1. I used the max function to identify the maximum number in the list and appended it to a separate blank list
2. I removed the maximum number from the original Unsorted list.
3. I iterated this process using a while loop to the extent of the number of elements in the list (This is arrived at by using the len() function on the list)
Using Sort function-
#We have used the build-in sort function and arranged the elements in descending order using reverse= True
Unsorted_list = [2,3,1,4,5]
Unsorted_list.sort(reverse= True)
print(Sorted_list)
Without Sort function-
Unsorted_list = [2,3,1,4,5]
Sorted_list = []
#Running a while loop which compares each element with the max value and appends in the sorted list created above
while Unsorted_list:
max = Unsorted_list[0]
for x in Unsorted_list:
if x > max:
max = x
Sorted_list.append(max)
Unsorted_list.remove(max)
print(Sorted_list)
Without function-
import numpy as np
Unsorted_list = [5,4,3,2,1]
sorted_list=[]
for i in np.arange(len(Unsorted_list)-1,-1,-1):
sorted_list.append(Unsorted_list[i])
print(sorted_list)
With function –
Unsorted_list = [5,4,3,2,1]
Unsorted_list.sort()
Unsorted_list= [2,3,1,4,5]
for i in range(len(Unsorted_list)):
for j in range(i + 1,len(Unsorted_list)):
if(Unsorted_list[i] < Unsorted_listt[j]):
#Swapping the numbers if they are greater:
temp = Unsorted_list[i]
Unsorted_list[i] = Unsorted_list[j]
Unsorted_list[j] = temp
sorted_list = Unsorted_list
print(sorted_list)
Using Sort function-
Unsorted_list = [2,3,1,4,5]
Unsorted_list.sort(reverse= True)
print(Sorted_list)
Without Sort function-
Unsorted_list = [2,3,1,4,5]
Sorted_list = []
while Unsorted_list:
max = Unsorted_list[0]
for x in Unsorted_list:
if x > max:
max = x
Sorted_list.append(max)
Unsorted_list.remove(max)
print(Sorted_list)
SORTING A LIST WITH SORT() FUCNTION:
INPUT::
list=[3,6,2,8,6,5,9]
list.sort()
print(list)
OUTPUT::
[2, 3, 5, 6, 6, 8, 9]
SORTING A LIST WITHOUT SORT() FUCNTION:
INPUT::
list=[3,6,2,8,6,5,9]
for i in range(0,len(list)):
for j in range(i+1,len(list)):
if list[i]>list[j]:
list[i],list[j]=list[j],list[i]
print(list)
OUTPUT::
[2, 3, 5, 6, 6, 8, 9]