package data_struct

func QSort(arr []MyData, l, r int) []MyData {
	result := make([]MyData, len(arr))
	copy(result, arr)
	qSort(result, l, r)
	return result
}

func qSort(arr []MyData, l, r int) []MyData {
	if l < r {
		s := Partition_Hoa(arr, l, r)
		arr = qSort(arr, l, s)
		arr = qSort(arr, s+1, r)
	}
	return arr
}

func Partition_Hoa(arr []MyData, l, r int) int {
	p := arr[(l+r)/2].Name
	i := l - 1
	j := r + 1

	for {
		for {
			i++
			if arr[i].Name >= p {
				break
			}
		}
		for {
			j--
			if arr[j].Name <= p {
				break
			}
		}

		if i >= j {
			return j
		}

		arr[i], arr[j] = arr[j], arr[i]
	}
}
