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
188,132 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)

–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,890 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,910 points

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,152 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
23,754 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,363 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,320 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
1,843 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