how to sort a list of numbers without using built-in functions like min max or any other function

+1 vote
Is it possible to sort a list of numbers without using built-in functions? If yes how to do it?
Jun 27, 2019 in Python by Arvind
• 3,050 points
189,476 views

4 answers to this question.

+2 votes

Yes it is possible. You can refer to the following code to understand it.

my_list = [-15, -26, 15, 1, 23, -64, 23, 76]
new_list = []

while my_list:
    min = my_list[0]  
    for x in my_list: 
        if x < min:
            min = x
    new_list.append(min)
    my_list.remove(min)    

print(new_list)
answered Jun 27, 2019 by Arvind
• 3,050 points
Min function is there
Sort a given List in ascending order without using any inbuilt Sorting Methods and Functions.?

Hey, 

You can try this:

NumList = []

Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

for i in range (Number):
    for j in range(i + 1, Number):
        if(NumList[i] > NumList[j]):
            temp = NumList[i]
            NumList[i] = NumList[j]
            NumList[j] = temp

print("Element After Sorting List in Ascending Order is : ", NumList)

It has a extra iteration, which can be avoided.

for i in range(0,num-1):
    for j in range(i+1, num):
        if(NumList[i]>NumList[j]):
            temp=NumList[i]
            NumList[i]=NumList[j]
            NumList[j]=temp
    print("Printing the list values after each iteration: ",NumList)
print("Sorted list: ",NumList)
Min is a variable here
Here append and remove are also inbuilt functions
"min" function? Where in the above example, can you please be more precise?
–1 vote

as far as I am aware you can just do:

list = [1, 3, 123, 1, 42, 123] # RANDOM NUMBERS
list.sort()

answered May 19, 2020 by anonymous
list.sort is a built in function

Hii,

Yes,sort() is a built in function in python.

The sort() method sorts the list ascending by default.

You can also make a function to decide the sorting criteria(s).

Syntax

list.sort(reverse=True|False, key=myFunc)

where,

reverse-Optional. reverse=True will sort the list descending. Default is reverse=False

key-Optional. A function to specify the sorting criteria(s)

Hope it is helpful!!
Thanks!!



 
Questions:how to sort a list of numbers without using built-in functions from command prompt.please try this code.
import sys
sortval=[]
val=sys.argv[1:]
print("val:",val)
for i in val:
    val=int(i)
    sortval.append(val)
print("before sortval:",sortval)
for i in range(len(sortval)):
    for j in range(i+1,len(sortval)):
        if(sortval[i]>sortval[j]):
            temp=sortval[i]
            sortval[i]=sortval[j]
            sortval[j]=temp
print("Aftersortval:",sortval)

0 votes

Python Program to Sort List in Ascending Order without using Sort. In this program, we are using Nested For Loop to iterate each number in a list, and sort them in ascending order.

 if(NumList[0] > NumList[1]) = if(67 > 86) – 

It means the condition is False. 

So, it exits from the If block, and the j value incremented by 1.

answered Dec 12, 2020 by Gitika
• 65,770 points
+1 vote

I guess you are trying to do something like this:

data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []

while data_list:
    minimum = data_list[0]  # arbitrary number in list 
    for x in data_list: 
        if x < minimum:
            minimum = x
    new_list.append(minimum)
    data_list.remove(minimum)    

print new_list
answered Dec 12, 2020 by Rajiv
• 8,870 points
.append() and .remove() are built-in methods
why cant we use 2 for loops

data_list=[4,1,3,2]
newlist=[]
for j in data_list:
    minm=data_list[0]
    for k in data_list:
        if k<minm:
            minm=k
    newlist.append(minm)
    data_list.remove(minm)
print(newlist)
minimum is still a function?
s=[5,1,6,9,7,2,11,3]

x=[]

l=len(s) #6

for i in range(l):

    for k in range(l-1):

        if( s[k] > s[k+1] ):

            temp=s[k]

            s[k]=s[k+1]

            s[k+1]=temp

print(s)
0 votes
a=[2,4,1,6]
b=[]
for i in range(len(a)):
  b.append(min(a))
  a.remove(min(a))
print(b)
answered Mar 21, 2021 by anonymous

edited Mar 5
0 votes
arr = [1, 5, 0, 0, 9, 10, 9, 3, 2, 3, 5,10]
new_arr = []
for index in range(len(arr)):
    index = len(new_arr) - index
    max_val = arr[index]
    for val in arr[index+1:]:
        if val >= max_val:
            max_val = val
    new_arr.append(max_val)
    arr.remove(max_val)
print(new_arr)
answered Jan 3, 2022 by vijay saw

edited Mar 5
0 votes
def fun(list):
    n=len(list)-1
    i=0
    flag=0
    if n<=0:
        return True
    else:
        while i < n:
            if list[i]<list[i+1]:
                i+=1
        
            else:
                flag=1
                break;
    if flag==0:
        return True
    else:
        return False
        
list=[10,20,30]
print(fun(list))

#for different test cases, take list as [10,5,20,40] or [10] or [] .etc.
answered May 18, 2022 by Gaurang Mishra

edited Mar 5
0 votes
my_list = [14, 8, 79, 45, 63, 52, 1, 98]
min_list = []
while my_list:
    minimum = min(my_list)
    min_list.append(minimum)
    my_list.remove(minimum)
print(min_list)
answered Aug 24, 2022 by Mathew Tomy

edited Mar 5
0 votes
ls= [3,1,5,9,7,2]

for i in range(len(ls)):
    for j in range(i + 1, len(ls)):
        if(ls[i] > ls[j]):
            temp = ls[i]
            ls[i] = ls[j]
            ls[j] = temp

print("Element After Sorting : ", ls)
answered Sep 20, 2022 by Yogesh K

edited Mar 5
0 votes

We can sort elements of list with swapping the numbers every time till the number is in its best place.

In terms of Time and Space complexity it would be the good solution, As we are using same list to sort and not iterating the numbers which are already sorted by the previous iteration.



nums = [0,2,1,2,0,2,0,1]
for ind_i, i in enumerate(nums):
for ind_j, j in enumerate(nums[ind_i+1:]):
if nums[ind_i]> nums[ind_i+ind_j+1]:
nums[ind_i], nums[ind_i+ind_j+1] = nums[ind_i+ind_j+1], nums[ind_i]

answered May 13, 2023 by Vijay

edited Mar 5

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14, 2019 in Python by Junaid
2,346 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
24,037 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,645 views
0 votes
1 answer
0 votes
1 answer

How to get a list of built in modules in Python?

simply open the python shell and type ...READ MORE

answered Aug 1, 2019 in Python by Arvind
• 3,050 points
1,549 views
0 votes
1 answer

How to check whether something is a variable or a function in Python?

Suppose you have a variable defined in ...READ MORE

answered Jul 30, 2019 in Python by Arvind
• 3,050 points
2,071 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP