Sort an array in a single loop using golang

Home » Problem Solving » Arrays » Sort an array in a single loop using golang

Sort is an common thing between programmers and mostly used as well, So today we are going to cover this how we can sort an array in just a single loop using left and right pointer and after getting each value we starts resets the loop to start again from the beginning to look for next values.

Given an array of size N, the task is to sort this array using a single loop.

Can the array the sorted using a single loop? 
Since all the known sorting methods use more than 1 loop, it is hard to imagine to do the same with a single loop. Practically, it is not impossible to do so. But doing so won’t be the most efficient. 

Example 1: Below code will sort an array with integer elements.

package main

import (
    "fmt"
)

func sort(arr []int) {
    for current := 0; current < len(arr)-1; current++ {
        if arr[current] > arr[current+1] {
            arr[current], arr[current+1] = arr[current+1], arr[current]
            current = -1      
    }
}

func main() {
    // non-reversed array
    arr := []int{1, 2, 45, 65, 32, 34}
    // output: {1,2,32,34,45,65}


    fmt.Printf("non reversed array: %d\n", arr)

    // calling recursiveReversed function
    sort(arr)

    fmt.Printf("reversed array: %d\n", arr)
}
Output:

non reversed array: [1 2 45 65 32 34]
reversed array: [1 2 32 34 45 65]

Example 2: Below code will sort the slice of string which contains numbers.

package main

import (
    "fmt"
)

func sort(arr []string) {
  for current := 0; current < len(arr)-1; current++ {
        if arr[current] > arr[current+1] {
            arr[current], arr[current+1] = arr[current+1], arr[current]
            current = -1
        }
    }
}


func main() {
    // non-reversed array
    arr := []string{"1", "2", "45", "65", "32", "34"}
    // output: {"1","2","32","34","45","65"}


    fmt.Printf("non reversed array: %s\n", arr)

    // calling recursiveReversed function
    sort(arr)

    fmt.Printf("reversed array: %s\n", arr)
}
Output:

non reversed array: [1 2 45 65 32 34]
reversed array: [1 2 32 34 45 65]

Example 3: sorting Alphabates string using the same method we did for slice of string []string.

package main

import (
    "fmt"
    "strings"
)

func sort(arr []string) {
  for current := 0; current < len(arr)-1; current++ {
        if arr[current] > arr[current+1] {
            arr[current], arr[current+1] = arr[current+1], arr[current]
            current = -1
        }
    }
}


func main() {

  s := "PROGRAMMINGEEKSCLUB"
  
  // non-reversed array
  arr := strings.Split(s,"")
  // output: [A B C E E G G I K L M M N O P R R S U]


  fmt.Printf("non reversed array: %s\n", arr)

  // calling recursiveReversed function
  sort(arr)
  
  fmt.Printf("reversed array: %s\n",arr)

}
Output:

non reversed array: [P R O G R A M M I N G E E K S C L U B]
reversed array: [A B C E E G G I K L M M N O P R R S U]

Is sorting array in single loop better than sorting in more than one loop?
Sorting in a single loop, though it seems to be better, is not an efficient approach. Below are some points to be taken into consideration before using single loop sorting: 

  • The time complexity of the sorting does not change in a single loop (in comparison to more than one loop sorting)
  • Single loop sorting shows that number of loops has little to do with time complexity of the algorithm.
  • Using a single loop only helps in shorter code

Thanks for reading 🙂

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.