1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

バカソート

Last updated at Posted at 2019-06-13
  • 最小値を探すことを繰り返すことでソートしていく
  • バカだから時間かかる
package main

import (
	"log"
	"math/rand"
	"time"
)

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

func main() {
	rand.Seed(time.Now().UnixNano())
	target := prepare(100000)
	log.Printf("Target: %#v\n", target)
	sorted := DumbSort(target)
	log.Printf("Sorted: %#v\n", sorted)
}

func prepare(size int) []int {
	ret := make([]int, size)
	for i := range ret {
		ret[i] = i
	}
	length := size - 1
	for i := length; i > 1; i -= 1 {
		r := rand.Intn(i)
		ret[i], ret[r] = ret[r], ret[i]
	}
	return ret
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?