Gadgets 4 Students Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
762 views
in RTU/BTU B.Tech(CSE-VI SEM) PYTHON LAB by Goeduhub's Expert (7.6k points)
Write a Program  to implement Selection sort

Where can I do online courses From Word's Top Instructors?

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]
Coursera:: Join For FREE

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Ques . Write a Program  to implement Selection sort

Answer : Selection sort is an sorting algorithm in which the array is divided into sorted array and unsorted array and we need to find the minimum element from unsorted array  and put it in a sorted array sequence .

Algorithm : 

  1. Set minimum element to index 0
  2. Search the minimum element in the array
  3. Swap with value at minimum index 
  4. Increment minimum to point to next element
  5. Repeat until list is sorted

Example : 

a = [86, 45, 6, 32, 5]

here we need to find the minimum element and place it in the beginning from i=0 to 4

5 45 6 32 86

here we need to find the minimum element and place it in the beginning from i=1 to 4

5 6 45 32 86

here we need to find the minimum element and place it in the beginning from i=2 to 4

5 6 32 45 86

here we need to find the minimum element and place it in the beginning from i=3 to 4

5 6 32 45 86

program : 

def selection_Sort(L):
   # i indicates how many items were sorted
   for i in range(len(L)-1):
        # To find the minimum value of the unsorted segment
        # We first assume that the first element is the lowest
        min_index = i
        # We then use j to loop through the remaining elements
        for j in range(i+1len(L)-1):
            # Update the min_index if the element at j is lower than it
            if L[j] < L[min_index]:
                min_index = j
        # After finding the lowest item of the unsorted regions, swap with the first unsorted item
        L[i], L[min_index] = L[min_index], L[i]
   return L

# Driver code to test above  
L = [801060407050
print("Unsorted list is:",L)
# Let's see the list after we run the Selection Sort
print("Sorted list is:",selection_Sort(L))

Output : 

Unsorted list is: [80, 10, 60, 40, 70, 50] 

Sorted list is: [10, 40, 60, 70, 80, 50]


For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here


3.3k questions

7.1k answers

395 comments

4.5k users

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...