This abstract class generalizes ways to sort an array

Introduced properties

type COMPARED: nullable Object

core :: Comparator :: COMPARED

What to compare to
fun bubble_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: bubble_sort

Bubble-sort array between from and to indices
abstract fun compare(a: COMPARED, b: COMPARED): Int

core :: Comparator :: compare

Compare a and b.
fun heap_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: heap_sort

Heap-sort array between from and to indices
fun insertion_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: insertion_sort

Insertion-sort array between from and to indices
fun is_sorted(seq: SequenceRead[COMPARED]): Bool

core :: Comparator :: is_sorted

Is seq sorted?
fun max(a: COMPARED, b: COMPARED): COMPARED

core :: Comparator :: max

Returns the maximum between a and b.
fun merge_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: merge_sort

Merge-sort array between from and to indices
fun min(a: COMPARED, b: COMPARED): COMPARED

core :: Comparator :: min

Returns the minimum between a and b.
fun quick_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: quick_sort

Quick-sort array between from and to indices
fun sort(array: Array[COMPARED])

core :: Comparator :: sort

Sort array using the compare function.

Redefined properties

redef type SELF: Comparator

core $ Comparator :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type COMPARED: nullable Object

core :: Comparator :: COMPARED

What to compare to
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun bubble_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: bubble_sort

Bubble-sort array between from and to indices
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun compare(a: COMPARED, b: COMPARED): Int

core :: Comparator :: compare

Compare a and b.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun heap_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: heap_sort

Heap-sort array between from and to indices
init init

core :: Object :: init

fun insertion_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: insertion_sort

Insertion-sort array between from and to indices
fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun is_sorted(seq: SequenceRead[COMPARED]): Bool

core :: Comparator :: is_sorted

Is seq sorted?
fun max(a: COMPARED, b: COMPARED): COMPARED

core :: Comparator :: max

Returns the maximum between a and b.
fun merge_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: merge_sort

Merge-sort array between from and to indices
fun min(a: COMPARED, b: COMPARED): COMPARED

core :: Comparator :: min

Returns the minimum between a and b.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun quick_sort(array: Array[COMPARED], from: Int, to: Int)

core :: Comparator :: quick_sort

Quick-sort array between from and to indices
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun sort(array: Array[COMPARED])

core :: Comparator :: sort

Sort array using the compare function.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram core::Comparator Comparator core::Object Object core::Comparator->core::Object core::MapComparator MapComparator core::MapComparator->core::Comparator core::DefaultComparator DefaultComparator core::DefaultComparator->core::Comparator core::DefaultReverseComparator DefaultReverseComparator core::DefaultReverseComparator->core::Comparator core::CachedAlphaComparator CachedAlphaComparator core::CachedAlphaComparator->core::Comparator poset::POSet POSet poset::POSet->core::Comparator functional::ComparatorWith ComparatorWith functional::ComparatorWith->core::Comparator vsm::IndexMatchSorter IndexMatchSorter vsm::IndexMatchSorter->core::DefaultComparator vsm::IndexMatchSorter... ... vsm::IndexMatchSorter...->vsm::IndexMatchSorter fca::ConceptLattice ConceptLattice fca::ConceptLattice->poset::POSet fca::ConceptLattice... ... fca::ConceptLattice...->fca::ConceptLattice

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class CachedAlphaComparator

core :: CachedAlphaComparator

Comparator that efficienlty use to_s to compare things
class ComparatorWith[E: nullable Object]

functional :: ComparatorWith

Comparator that use a function provided by the user to compare between elements.
class DefaultComparator

core :: DefaultComparator

This comparator uses the operator <=> to compare objects.
class DefaultReverseComparator

core :: DefaultReverseComparator

This comparator uses the operator <=> to compare objects in a reverse order.
class MapComparator[K: nullable Object, V: nullable Object]

core :: MapComparator

A comparator that compares things with their values in a map.
class POSet[E: nullable Object]

poset :: POSet

Pre-order set graph.

Descendants

class ConceptLattice[O: Object, A: Object]

fca :: ConceptLattice

Concept Lattice
class IndexMatchSorter

vsm :: IndexMatchSorter

Sort matches by similarity

Class definitions

core $ Comparator
# This abstract class generalizes ways to sort an array
interface Comparator
	# What to compare to
	type COMPARED: nullable Object

	# Compare `a` and `b`.
	#
	# Returns:
	#
	# * -1 if a < b
	# * 0  if a = b
	# * 1  if a > b
	fun compare(a: COMPARED, b: COMPARED): Int is abstract

	# Is `seq` sorted?
	#
	#     assert default_comparator.is_sorted([1,2,2,3])   == true
	#     assert default_comparator.is_sorted([1,10,2,3])  == false
	#     assert alpha_comparator.is_sorted([1,10,2,3])    == true
	fun is_sorted(seq: SequenceRead[COMPARED]): Bool
	do
		if seq.length <= 1 then return true
		var prev = seq.first
		for e in seq do
			if compare(prev, e) > 0 then return false
			prev = e
		end
		return true
	end

	# Returns the minimum between `a` and `b`.
	#
	#     assert default_comparator.min(2,10) == 2
	#     assert alpha_comparator.min(2,10)   == 10
	#
	# If both are equivalent, then returns `a`.
	#
	#     var m = alpha_comparator.min(1, "1")
	#     assert m == 1
	#     assert m != "1"
	fun min(a,b: COMPARED): COMPARED
	do
		if compare(a,b) > 0 then return b else return a
	end

	# Returns the maximum between `a` and `b`.
	#
	#     assert default_comparator.max(2,10) == 10
	#     assert alpha_comparator.max(2,10)   == 2
	#
	# If both are equivalent, then returns `a`.
	#
	#     var m = alpha_comparator.max(1, "1")
	#     assert m == 1
	#     assert m != "1"
	fun max(a,b: COMPARED): COMPARED
	do
		if compare(a,b) < 0 then return b else return a
	end

	# Sort `array` using the `compare` function.
	#
	#     var a = [10, 2, 3, 1, 4]
	#     default_comparator.sort(a)
	#     assert a == [1, 2, 3, 4, 10]
	#     alpha_comparator.sort(a)
	#     assert a == [1, 10, 2, 3, 4]
	fun sort(array: Array[COMPARED]) do sub_sort(array, 0, array.length-1)

	# Sort `array` between `from` and `to` indices
	private fun sub_sort(array: Array[COMPARED], from: Int, to: Int)
	do
		if from >= to then
			return
		else if from + 7 < to then
			quick_sort(array, from, to)
		else
			bubble_sort(array, from, to)
		end
	end

	# Quick-sort `array` between `from` and `to` indices
	# Worst case: O(n^2), Average case: O(n lg n)
	#
	#     var a = [5, 2, 3, 1, 4]
	#     default_comparator.quick_sort(a, 0, a.length - 1)
	#     assert a == [1, 2, 3, 4, 5]
	#     var a2 = new Array[Int]
	#     default_comparator.quick_sort(a2, 0, a2.length - 1)
	#     assert a2 == new Array[Int]
	#     var a3 = [1]
	#     default_comparator.quick_sort(a3, 0, a3.length - 1)
	#     assert a3 == [1]
	fun quick_sort(array: Array[COMPARED], from: Int, to: Int) do
		if from >= to then return
		var pivot = array[from]
		var i = from
		var j = to
		while j > i do
			while i <= to and compare(array[i], pivot) <= 0 do i += 1
			while j > i and compare(array[j], pivot) >= 0 do j -= 1
			if j > i then
				var t = array[i]
				array[i] = array[j]
				array[j] = t
			end
		end
		array[from] = array[i-1]
		array[i-1] = pivot
		sub_sort(array, from, i-2)
		sub_sort(array, i, to)
	end

	# Bubble-sort `array` between `from` and `to` indices
	# Worst case: O(n^2), average case: O(n^2)
	#
	#     var a = [5, 2, 3, 1, 4]
	#     default_comparator.bubble_sort(a, 0, a.length - 1)
	#     assert a == [1, 2, 3, 4, 5]
	fun bubble_sort(array: Array[COMPARED], from: Int, to: Int)
	do
		var i = from
		while i < to do
			var min = i
			var min_v = array[i]
			var j = i
			while j <= to do
				if compare(min_v, array[j]) > 0 then
					min = j
					min_v = array[j]
				end
				j += 1
			end
			if min != i then
				array[min] = array[i]
				array[i] = min_v
			end
			i += 1
		end
	end

	# Insertion-sort `array` between `from` and `to` indices
	# Worst case: O(n^2), average case: O(n^2)
	#
	#     var a = [5, 2, 3, 1, 4]
	#     default_comparator.insertion_sort(a, 0, a.length - 1)
	#     assert a == [1, 2, 3, 4, 5]
	fun insertion_sort(array: Array[COMPARED], from: Int, to: Int) do
		for i in [from..to] do
			var j = i
			while j > 0 and compare(array[j], array[j - 1]) < 0 do
				array.swap_at(j, j - 1)
				j -= 1
			end
		end
	end

	# Merge-sort `array` between `from` and `to` indices
	# Worst case: O(n lg n), average: O(n lg n)
	#
	#     var a = [5, 2, 3, 1, 4]
	#     default_comparator.merge_sort(a, 0, a.length - 1)
	#     assert a == [1, 2, 3, 4, 5]
	fun merge_sort(array: Array[COMPARED], from, to: Int) do
		if from >= to then return
		var mid = (to + from) / 2
		merge_sort(array, from, mid)
		merge_sort(array, mid + 1, to)
		merge(array, from, mid, to)
	end

	private fun merge(array: Array[COMPARED], from, mid, to: Int) do
		var l = new Array[COMPARED]
		for i in [from..mid] do l.add array[i]
		var r = new Array[COMPARED]
		for i in [mid + 1..to] do r.add array[i]
		var i = 0
		var j = 0
		for k in [from..to] do
			if i >= l.length then
				array[k] = r[j]
				j += 1
			else if j >= r.length then
				array[k] = l[i]
				i += 1
			else if compare(l[i], r[j]) <= 0 then
				array[k] = l[i]
				i += 1
			else
				array[k] = r[j]
				j += 1
			end
		end
	end

	# Heap-sort `array` between `from` and `to` indices
	# Worst case: O(n lg n), average: O(n lg n)
	#
	#     var a = [5, 2, 3, 1, 4]
	#     default_comparator.heap_sort(a, 0, a.length - 1)
	#     assert a == [1, 2, 3, 4, 5]
	fun heap_sort(array: Array[COMPARED], from, to: Int) do
		var size = build_heap(array)
		for j in [from..to[ do
			array.swap_at(0, size)
			size -= 1
			heapify(array, 0, size)
		end
	end

	private fun build_heap(array: Array[COMPARED]): Int do
		var size = array.length - 1
		var i = size / 2
		while i >= 0 do
			heapify(array, i, size)
			i -= 1
		end
		return size
	end

	private fun heapify(array: Array[COMPARED], from, to: Int) do
		var l = from * 2
		var r = l + 1
		var largest: Int
		if l < to and compare(array[l], array[from]) > 0 then
			largest = l
		else
			largest = from
		end
		if r < to and compare(array[r], array[largest]) > 0 then
			largest = r
		end
		if largest != from then
			array.swap_at(from, largest)
			heapify(array, largest, to)
		end
	end

end
lib/core/collection/sorter.nit:22,1--260,3