Selection sort algorithm (Selection Sort)

What is Selection Sort?

Selection Sort is a simple algorithm. This sorting algorithm is an algorithm based on in-place comparison, in which the list is divided into two parts, sorted (list) on the left and unsorted (unsorted list) in the right. Initially, the sorted part is blank and the unordered part is the original list.

The smallest element selected from the array has not been sorted and is swapped with the most left part and that element becomes the element of the sorted array. This process continues until all of the unmatched array elements are moved to the sorted array.

This algorithm is not suitable for large data sets where the worst case complexity and the average case for O (n2) with n are the number of elements.

You learn the in-place concept in chapter: Some basic concepts about sorting algorithms.

Selection sort works (Selection Sort) works

Below are illustrations of how the algorithm works. Suppose we have an array as follows:

Selection sort algorithm (Selection Sort) Picture 1

From the first position in the list has been sorted, the entire list is approved continuously. The first position has a value of 14, we find the entire list and find that 10 is the smallest value.

Selection sort algorithm (Selection Sort) Picture 2

Therefore, we replace 14 with 10. After a loop, value 10 replaces the value 14 at the first position in the sorted list. We swap these two values.

Selection sort algorithm (Selection Sort) Picture 3

In second place, value 33, we continue to scan the rest of the list in order of each element.

Selection sort algorithm (Selection Sort) Picture 4

We see that 14 is the second smallest value in the list and it should appear in the second position. We swap these two values.

Selection sort algorithm (Selection Sort) Picture 5

After two loops, the two smallest values ​​have been placed at the beginning of the sorted list.

Selection sort algorithm (Selection Sort) Picture 6

The same process will apply to the rest of the list. The figures below illustrate these processes.

Selection sort algorithm (Selection Sort) Picture 7

Next we will monitor some other aspects of the selection algorithm.

Selection sort algorithm (Selection Sort)

 Bước 1 : Thiết lập MIN về vị trí 0 Bước 2 : Tìm kiếm phần tử nhỏ nhất trong danh sách Bước 3 : Tráo đổi với giá trị tại vị trí MIN Bước 4 : Tăng MIN để trỏ tới phần tử tiếp theo Bước 5 : Lặp lại cho tới khi toàn bộ danh sách đã được sắp xếp

Sample algorithm for sort selection

 B ắ t đầ u gi ả i thu ậ t s ắ p x ế p ch ọ n ( Selection Sort ) list : m ả ng c á c ph ầ n t ử n : k í ch c ỡ m ả ng for i = 1 t ớ i n - 1 /* thiết lập phần tử hiện tại là min*/ min = i /* kiểm tra phần tử có là nhỏ nhất không */ for j = i + 1 t ớ i n if list [ j ] < list [ min ] th ì min = j ; k ế t th ú c if k ế t th ú c for /* tráo đổi phần tử nhỏ nhất với phần tử hiện tại*/ if indexMin != i then tr á o đổ i list [ min ] v à list [ i ] k ế t th ú c if k ế t th ú c for K ế t th ú c gi ả i thu ậ t 

According to Tutorialspoint

Previous lesson: Insert algorithm (Insertion Sort)

Next lesson: Mixing algorithm (Merge Sort)

5 ★ | 1 Vote

May be interested

  • How does YouTube algorithm work?How does YouTube algorithm work?
    have you ever wondered how youtube algorithm works?
  • Panda 4.0 algorithm was updated by Google on May 20, 2014Panda 4.0 algorithm was updated by Google on May 20, 2014
    google's panda algorithm was created to block websites with poor quality content, helping google improve the user experience on the world's largest search engine.
  • Sort information on FacebookSort information on Facebook
    in the past, users could lose posts when they ignored the stream, especially if you gave facebook a large amount of time to access every day.
  • Sort names alphabetically in WordSort names alphabetically in Word
    you are asked by your boss to organize the list of employees alphabetically and not sure what to do? this article will show you how to sort names by abc
  • Sort data in ExcelSort data in Excel
    sort data in excel - excel supports you with data sorting tools to give you the sorting options. you can use the sort tool to sort the data in your excel document.
  • How to sort data in Excel using Sort is extremely simpleHow to sort data in Excel using Sort is extremely simple
    with just a few operations to arrange data in excel, you can transform your spreadsheet to become neat and easy to see. let's find out with free download now!
  • Greedy Algorithm (Greedy Algorithm)Greedy Algorithm (Greedy Algorithm)
    greedy algorithm is a combination optimization algorithm. search algorithms, select local optimal solutions in each step in the hope of finding a global optimal solution.
  • How to sort by multiple columns in Google SheetsHow to sort by multiple columns in Google Sheets
    when you are analyzing complex data sets in google sheets, you can take advantage of the built-in sorting feature to organize your data. you can sort in columns or for more complex data, you can sort in multiple columns.
  • Introducing the greedy algorithmIntroducing the greedy algorithm
    in this article, i will introduce a rather interesting algorithm called greedy algorithm and its detailed solution.
  • MS Excel 2007 - Lesson 8: Sort and FilterMS Excel 2007 - Lesson 8: Sort and Filter
    sort and filter are features that allow you to manipulate data in a spreadsheet based on standards. to sort data and filter data in excel 2007 you will use the sort & filter feature. below tipsmake.com will guide you how to filter data, arrange data with this feature in excel 2007.