Skip to content

Instantly share code, notes, and snippets.

View educartoons's full-sized avatar

Eduar educartoons

View GitHub Profile
@educartoons
educartoons / quickSort.swift
Created June 25, 2020 02:26
Implementation of Quick Sort in Swift
// var numbers: [Int] = [15, 67, 8, 16, 44, 27, 12, 35];
// We will call quicksort with
// quickSort(&numbers, p: 0, r: numbers.count-1);
func quickSort(_ numbers: inout [Int], p: Int, r: Int) -> Void{
if p<r {
let q = partition(&numbers, p: p, r: r)
quickSort(&numbers, p: p, r: q-1);
quickSort(&numbers, p: q+1, r: r)
}
@educartoons
educartoons / quickSort.go
Created June 24, 2020 04:34
Implementation of Quick Sort in Go
// We will call quicksort with
// quickSort(array, 0, len(array)-1)
func quickSort(A []int, p int, r int) []int {
if p < r {
var q = partition(A, p, r)
quickSort(A, p, q-1)
quickSort(A, q+1, r)
}
return A
@educartoons
educartoons / quickSort.ts
Created June 24, 2020 04:32
Implementation of Quick Sort in Typescript
// We will call quicksort with p: 0 and r: Array.length - 1
// console.log(quickSort(Array, 0, B.length - 1));
function quickSort(A: number[], p: number, r: number): number[] {
if (p < r) {
const q = partition(A, p, r);
quickSort(A, p, q - 1);
quickSort(A, q + 1, r);
}
return A;
@educartoons
educartoons / quickSort.cpp
Last active June 24, 2020 04:34
Implementation of Quick Sort in C++
// We will call the function with quicksort(numbers, 1, numbers.length - 1)
int partition(int *numbers, int p, int r){
int x = numbers[r];
int i = p - 1;
for (int j = p; j<r; j++) {
if(numbers[j]<=x){
i = i + 1;
swap(numbers[i], numbers[j]);
}
@educartoons
educartoons / insertion-sort.cpp
Created June 23, 2020 22:53
Implementation of Insertion Sort in C++
// numbers = array of numbers
// size = length of numbers
void insertionSort(int *numbers,int size){
int i, j, key;
for (j=1; j<size; j++) {
key = numbers[j];
i = j - 1;
while(i>=0 and numbers[i]>key){
numbers[i+1] = numbers[i];
@educartoons
educartoons / insertion-sort.go
Created June 23, 2020 22:52
Implementation of Insertion Sort in Go
func insertionSort(A []int) []int {
for j := 0; j < len(A); j++ {
var key = A[j]
var i = j - 1
for i >= 0 && A[i] > key {
A[i+1] = A[i]
i = i - 1
}
A[i+1] = key
@educartoons
educartoons / insertion-sort.ts
Created June 23, 2020 22:52
Implementation of Insertion Sort in Typescript
function insertionSort(A: number[]): number[] {
for (let j = 1; j < A.length; j++) {
let key = A[j];
let i = j - 1;
while (i >= 0 && A[i] > key) {
A[i + 1] = A[i];
i = i - 1;
}
A[i + 1] = key;
}
@educartoons
educartoons / selection-sort.ts
Created June 18, 2020 01:52
Implementation of Selection Sort in Typescript
function selectionSort(A: number[]): number[] {
for (let i = 0; i < A.length - 1; i++) {
let min = i;
for (let j = i + 1; j < A.length; j++) {
if (A[j] < A[min]) {
min = j;
}
}
[A[min], A[i]] = [A[i], A[min]]
}
@educartoons
educartoons / selection-sort.go
Created June 18, 2020 01:51
Implementation of Selection Sort in Go
func selectionSort(A []int) []int {
for i := 0; i < len(A)-1; i++ {
var min = i
for j := i + 1; j < len(A); j++ {
if A[j] < A[min] {
min = j
}
}
A[min], A[i] = A[i], A[min]
}
@educartoons
educartoons / bubbleSort.go
Created June 15, 2020 22:44
Implementation of Bubble Sort in Go
func bubbleSort(A []int) []int {
for i := 1; i < len(A); i++ {
for j := len(A) - 1; j >= i; j-- {
if A[j-1] > A[j] {
A[j-1], A[j] = A[j], A[j-1]
}
}
}
return A