Alternative Sorting in Golang

In this article we are going to solve a problem using go programming language, and the supporting package we are going to use in the code is sort package of golang which is helpful for sorting strings,arrays etc.

Problem

Given an array of i…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by kuldeep_singh

In this article we are going to solve a problem using go programming language, and the supporting package we are going to use in the code is sort package of golang which is helpful for sorting strings,arrays etc.

Problem

Given an array of integers, print the array in such a way that the first element is first maximum number and second element is first minimum number and so on.

Examples

Input : arr[] = {6, 3, 1, 2, 4, 5, 7}
Output : 7 1 6 2 5 3 4

Input : arr[] = {1, 6, 9, 4, 3, 7, 8, 2}
Output : 9 1 8 2 7 3 6 4

There are two ways we can print the number in the required order.

  1. A simple solution is to first print maximum element, then minimum, then second maximum, and so on. Time complexity of this approach is O(n2).

  2. An efficient solution involves following steps.

So we are going to implement the efficient solution in this.

Approach

  1. Sort input array using a O(n Log n) algorithm.
  2. We maintain two pointers, one from beginning and one from end in sorted array. We alternatively print elements pointed by two pointers and move them toward each other.
/* 
Alternative sorting problem
EXAMPLE
Input : arr[] = {6, 3, 1, 2, 4, 5, 7}
Output : 7 1 6 2 5 3 4
Input : arr[] = {1, 6, 9, 4, 3, 7, 8, 2}
Output : 9 1 8 2 7 3 6 4
*/
package main
import (
    "fmt"
  "sort"
)
func alternativeSorting(arr []int) {
  sort.Ints(arr)
  fmt.Println("Sorted Array",arr)
  left := 0
  right := len(arr)-1
  for left < right {
     fmt.Printf("%d %d ",arr[right],arr[left])
     left++
     right--
  }
  if len(arr) % 2 != 0 {
    fmt.Printf("%d ",arr[left])
  }
}

func main() {
  arr := []int{12, 1, 6, 4, 7, 10}
  fmt.Println("Before",arr)
  alternativeSorting(arr)
  fmt.Println()
}

Output:

Before [1 12 4 6 7 10]
Sorted Array [1 4 6 7 10 12]
12 1 10 4 7 6

you can visit my personal blog for this type of content.

Thanks for Reading


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by kuldeep_singh


Print Share Comment Cite Upload Translate Updates
APA

kuldeep_singh | Sciencx (2022-10-05T11:41:53+00:00) Alternative Sorting in Golang. Retrieved from https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/

MLA
" » Alternative Sorting in Golang." kuldeep_singh | Sciencx - Wednesday October 5, 2022, https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/
HARVARD
kuldeep_singh | Sciencx Wednesday October 5, 2022 » Alternative Sorting in Golang., viewed ,<https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/>
VANCOUVER
kuldeep_singh | Sciencx - » Alternative Sorting in Golang. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/
CHICAGO
" » Alternative Sorting in Golang." kuldeep_singh | Sciencx - Accessed . https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/
IEEE
" » Alternative Sorting in Golang." kuldeep_singh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/. [Accessed: ]
rf:citation
» Alternative Sorting in Golang | kuldeep_singh | Sciencx | https://www.scien.cx/2022/10/05/alternative-sorting-in-golang/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.