{"id":37836,"date":"2021-06-29T10:16:34","date_gmt":"2021-06-29T08:16:34","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=37836"},"modified":"2021-06-30T09:03:59","modified_gmt":"2021-06-30T07:03:59","slug":"types-of-sorting-algorithms-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/tipos-de-algoritmos-de-ordenacion-en-python\/","title":{"rendered":"Sorting algorithms in Python"},"content":{"rendered":"<p>Sometimes we find that we have the data that we store or retrieve from an application out of order, without any logical order, and we need to organize and\/or classify it to process it correctly or use it more efficiently.<\/p>\n\n\n\n<p>With the <a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/eiposgrados.com\/master-en-python\/\">Master of Advanced Programming in Python, Hacking, Big Data and Machine Learning<\/a> You will be able to become a widely qualified expert to carry out programming work in Python specialized in cutting-edge areas such as Big Data, Hacking and Machine Learning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of sorting algorithms in Python<\/h2>\n\n\n\n<p>Today we are going to learn about the most common sorting algorithms:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Bubble Sort<\/strong><\/li><li><strong>Selection order (Sort Selection)<\/strong><\/li><li><strong>Insertion Type (Insert Sort)<\/strong><\/li><li><strong>Merge Sort<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Bubble Sort<\/h3>\n\n\n\n<p>This simple sorting algorithm iterates over the list of data, comparing elements in pairs until the largest elements<strong> \u201cbubble\u201d<\/strong> to the end of the list and the smallest ones remain at the beginning.<\/p>\n\n\n\n<p>Start by comparing the first two<strong> list items<\/strong>, if the first element is greater than the second, we exchange them, if not, they stay as they are. Then we move on to the next pair of elements, compare them and swap if necessary.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-en-python-1024x473.png\" alt=\"types of sorting algorithms in python\" class=\"wp-image-37837\" width=\"753\" height=\"348\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-en-python-1024x473.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-en-python-300x139.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-en-python-768x355.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-en-python.png 1406w\" sizes=\"(max-width: 753px) 100vw, 753px\" \/><\/figure><\/div>\n\n\n\n<p>The while loop breaks when no elements have been swapped.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Selection order (Sort Selection)<\/h3>\n\n\n\n<p>This algorithm <strong>separates the list into two parts<\/strong>, ordered and not ordered. It continually \u201cremoves\u201d the smallest element from the unsorted part and adds it to the sorted part.<\/p>\n\n\n\n<p>What this algorithm actually does is treat the left part of the list as the sorted part by searching the entire list for the smallest element and putting it first. Then, knowing that we already have the <strong>smallest element first<\/strong>, we search the entire list for the smallest of the remaining unordered elements and exchange it with the next ordered one and so on until the list is finished.<\/p>\n\n\n\n<p>The more elements we have ordered, the fewer elements we will have to <strong>examine.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion-1024x606.png\" alt=\"\" class=\"wp-image-37838\" width=\"706\" height=\"418\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion-1024x606.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion-300x178.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion-768x455.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion-279x165.png 279w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-de-ordenacion.png 1402w\" sizes=\"(max-width: 706px) 100vw, 706px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Insertion Type (Insert Sort)<\/h3>\n\n\n\n<p>This algorithm, like selection sorting, s<strong>eparates the list into two parts, ordered and unordered. <\/strong>We also assume that the first element is sorted, then we go to the next element that we are going to call X, we compare we insert X as first.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-python-1024x747.png\" alt=\"\" class=\"wp-image-37841\" width=\"625\" height=\"455\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-python-1024x747.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-python-300x219.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-python-226x165.png 226w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-python.png 1392w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Merge Sort<\/h3>\n\n\n\n<p>This algorithm starts <strong>splitting the list into two, then those two halves into 4 and so on<\/strong> until we have lists one element long. These elements are then put back together in order. We&#039;ll first merge the individual elements into pairs again by sorting them with each other, then we&#039;ll continue sorting them into groups until we have a single sorted list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-733x1024.png\" alt=\"\" class=\"wp-image-37873\" width=\"654\" height=\"914\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-733x1024.png 733w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-215x300.png 215w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-768x1073.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-1100x1536.png 1100w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python-118x165.png 118w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/algoritmos-master-en-python.png 1150w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/figure><\/div>\n\n\n\n<p>He<strong> result <\/strong>with any of the algorithms it would be the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/listas-ordenadas.png\" alt=\"\" class=\"wp-image-37840\" width=\"513\" height=\"117\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/listas-ordenadas.png 710w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/06\/listas-ordenadas-300x68.png 300w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>We are going to learn about the most common types of sorting algorithms in Python and the implementation of each of them. It&#039;s very interesting!<\/p>","protected":false},"author":51,"featured_media":37863,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-37836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/37836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=37836"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/37836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/37863"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=37836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=37836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=37836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}