Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Starting with sorting algorithms
Ricardo Borges
Ricardo Borges

Posted on • Edited on • Originally published atricardoborges.dev

     

Starting with sorting algorithms

A sorting algorithm is an algorithm that sorts elements in a list into an order, among other uses, sorting can be applied to prepare a set of data for another algorithm, for example, Binary Search.

In this post, I'll describe three sorting algorithms that, although not the most efficient, are easy to understand and are in-place algorithms, meaning that they don't require auxiliary data structures.

Bubble Sort

Bubble sort starts at the beginning of the list, comparing each pair of adjacent elements, if the first is greater than the second, it swaps them. Then it starts again at the beginning of the list and repeats this process until no swap occurred on the last pass.

bubble sort

functionswap(arr:number[],i:number,j:number){[arr[i],arr[j]]=[arr[j],arr[i]];}functionbubbleSort(arr:number[]){for(leti=0;i<arr.length-1;i++){letswapped=false;for(letj=0;j<arr.length-1;j++){if(arr[j]>arr[j+1]){swap(arr,j,j+1);swapped=true;}}if(!swapped)break;}}constarr=[5,3,1,2,4];bubbleSort(arr);console.log(arr);
Enter fullscreen modeExit fullscreen mode

Since its time complexity is Ο(n^2) on average and worst cases, this algorithm is more suitable for small or nearly ordered data sets.

Selection Sort

The list is divided into a sorted part at the left and an unsorted part at the right in this algorithm. Initially, the sorted sublist is empty and the unsorted one is all the list. Then it searches for the smallest element in the unsorted sublist and swaps it with the leftmost unsorted element, moving the sublist boundaries one element to the right.

selection sort

functionselectionSort(arr:number[]){for(leti=0;i<arr.length-1;i++){letmin=i;for(letj=i+1;j<arr.length;j++){if(arr[j]<arr[min]){min=j;}}swap(arr,min,i);}}
Enter fullscreen modeExit fullscreen mode

Like Bubble Sort, this algorithm has a quadratic time complexity Ο(n^2), so it is also more suitable for small or nearly ordered data sets. However, Selection Sort performs fewer swaps than Bubble Sort.

Insertion Sort

Insertion Sort keeps a sorted sublist at the beginning of the list, and for each element from the list, it searches for the right position in that sublist toinsert that element.

insertion sort

functioninsertionSort(arr:number[]){for(leti=1;i<arr.length;i++){letvalue=arr[i];letj=i-1;while(j>=0&&arr[j]>value){arr[j+1]=arr[j];j=j-1;}arr[j+1]=value;}}
Enter fullscreen modeExit fullscreen mode

Insertion sort has a quadratic time complexity for average and worst cases too, however, it's the fastest sorting algorithm for small lists.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

An enthusiastic software engineer who writes about what he learns
  • Location
    Portugal
  • Education
    Bachelor of Software Engineering
  • Work
    Software Engineer
  • Joined

More fromRicardo Borges

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp