Software Engineer Handbook
  • 👋Welcome to this handbook
  • Level 0 : Non-technical considerations
    • How to manage your time effectively
    • How to prepare for job interviews #1 : Digital Presence
    • How to prepare for job interviews #2 : At the interview
  • Level 1 : Computer Science
    • Intro: Why computer science is critical
    • Algorithms & Data structures Handbook
      • Algorithms & Data structures Handbook
      • Sorting Algorithms
      • Data Structures ADTs Implementation (In Go)
      • Techniques for Solving Data Structures Problems
    • Computer Organization and Architecture
    • Operating Systems
    • Database systems
  • Level 2 : System Design
    • Step 1 : Analysis
    • Step 2 : Digging deeper into data modeling
    • Step 3 : High-Level component design
    • Step 4: Low-Level / Detailed Design system
      • Clients
      • Application Servers
      • Databases
      • Security
      • Distributed Systems with Microservices
  • Level 4: Productivity Handbook for Software Engineers
    • How I use Fig
    • Command line tools
    • Develop on the cloud
  • Extra
  • Leadership
    • Processes
    • Poeple
    • Delivery
    • Transitioning to a new role
    • Resources I like
Powered by GitBook
On this page
  • Bubble Sort
  • Merge Sort
  • Insertion Sort
  1. Level 1 : Computer Science
  2. Algorithms & Data structures Handbook

Sorting Algorithms

Bubble Sort

func bubbleSort(arr []int ) []int {
	for i:= 0 ; i < len(arr) - 1 ; i++ {
		for j:=0 ; j < len(arr) - 1 - i ; j++ {
			if arr[j] > arr[j + 1] {
				arr[j], arr[j+1] = arr[j+1],arr[j]
			} 		
		} 
	}
	return arr
}

Merge Sort

package main 

import (
	"fmt"
)

func main(){
  fmt.Println("Merge Sort:")
  fmt.Println(mergeSort([]int{2,1,-3,0}))
  fmt.Println(mergeSort([]int{22,1111,-113,10,6}))
  fmt.Println(mergeSort([]int{212,16,-1,110,6}))
}

func mergeSort(arr []int)[]int{
	
	n := len(arr)

	if n == 1 {
		return arr
	}
	
	mid := int(n / 2)
	
	right := arr[0: mid]	
	left := arr[mid : n]
	
	return merge(mergeSort(right), mergeSort(left))
	
}

func merge(right[]int, left[]int) []int{
	sorted := make([]int, len(right) + len(left))
	
	i := 0
	for len(right) > 0  && len(left) > 0 {
		if right[0] < left[0]{
			sorted[i] = right[0]
			right = right[1:]
		}else {
			sorted[i] = left[0]
			left=left[1:]
		}
		i++
	}

	for j:= 0; j < len(right) ; j++ {
		sorted[i] =  right[j]
		i++
	}

	for j:= 0 ; j < len(left) ; j++ {
		sorted[i] = left[j]
		i++
	}

	return sorted
}

Insertion Sort

package main 

import (
	"fmt"
)

func main(){
	  fmt.Printf("Inerstion sort: %d", insertionSort([]int{2,3,-1,-4,9,1,2,0}))
}

// Take j and compare it with all the prevous elements
// We assume that the prevous eleemnts is the biggest elements in the list (ascdening)
func insertionSort(arr[]int)[]int{
	for i:= 1 ; i < len(arr) ; i++{
		j:=i
		for j > 0 && arr[j] < arr[j-1]{
			arr[j], arr[j-1] = arr[j-1], arr[j]
			j--
		}
	}

	return arr
}
PreviousAlgorithms & Data structures HandbookNextData Structures ADTs Implementation (In Go)

Last updated 2 years ago