diff --git a/QSort.java b/QSort.java index 635e5dd..3e7d33e 100644 --- a/QSort.java +++ b/QSort.java @@ -1,29 +1,33 @@ -public class QuickSort -{ public static void main(String args[]) - { int a[]={5,7,4,3,6,8,9,1,2,0}; - QuickSort obj=new QuickSort(); - for(int i=0; i= 1) - { int pivot = array[start]; - while (k > i) - { while (array[i] <= pivot && i <= end && k > i) i++; - while (array[k] > pivot && k >= start && k >= i) k--; - if (k > i) swap(array, i, k); - } - swap(array, start, k); - quickSort(array, start, k - 1); // quicksort the left partition - quickSort(array, k + 1, end); // quicksort the right partition - } - }//quickSort() - void swap(int array[], int index1, int index2) - { int temp = array[index1]; - array[index1] = array[index2]; - array[index2] = temp; - }//swap() +public class QuickSort { + public static void main(String args[]) { + int a[] = {5, 7, 4, 3, 6, 8, 9, 1, 2, 0}; + QuickSort obj = new QuickSort(); + for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); + System.out.println(); + obj.quickSort(a, 0, a.length - 1); + for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); + System.out.println(); + } + + void quickSort(int array[], int start, int end) { + int i = start; + int k = end; + if (end - start >= 1) { + int pivot = array[start]; + while (k > i) { + while (array[i] <= pivot && i <= end && k > i) i++; + while (array[k] > pivot && k >= start && k >= i) k--; + if (k > i) swap(array, i, k); + } + swap(array, start, k); + quickSort(array, start, k - 1); // quicksort the left partition + quickSort(array, k + 1, end); // quicksort the right partition + } + }//quickSort() + + void swap(int array[], int index1, int index2) { + int temp = array[index1]; + array[index1] = array[index2]; + array[index2] = temp; + }//swap() }//class \ No newline at end of file diff --git a/QuickSort.c b/QuickSort.c index 5ace572..e8480ea 100755 --- a/QuickSort.c +++ b/QuickSort.c @@ -1,51 +1,51 @@ -#include - -void swap(int *x,int *y) -{ - int temp; - temp=*x; - *x=*y; - *y=temp; -} - -int part(int a[],int l,int r) -{ - if(l>=r) - return -1; - int i=l,j=r-1; - while(i<=j) - { - if(i<=j && a[i]>=a[r]){ - swap(&a[i],&a[j]); - j--;} - else i++; - } - swap(&a[i],&a[r]); - return i; -} - -void sort(int a[],int l,int r) -{ - int x; - if(l>r)return; - x=part(a,l,r); - if(x==-1)return; - sort(a,l,x-1); - sort(a,x+1,r); - return ; -} - -int main() -{ - int n,a[100],i; - scanf("%d",&n); - for(i=0;i + +void swap(int *x,int *y) +{ + int temp; + temp=*x; + *x=*y; + *y=temp; +} + +int part(int a[],int l,int r) +{ + if(l>=r) + return -1; + int i=l,j=r-1; + while(i<=j) + { + if(i<=j && a[i]>=a[r]){ + swap(&a[i],&a[j]); + j--;} + else i++; + } + swap(&a[i],&a[r]); + return i; +} + +void sort(int a[],int l,int r) +{ + int x; + if(l>r)return; + x=part(a,l,r); + if(x==-1)return; + sort(a,l,x-1); + sort(a,x+1,r); + return ; +} + +int main() +{ + int n,a[100],i; + scanf("%d",&n); + for(i=0;i m: - more = append(more, item) - } - } - - less, more = QuickSort(less), QuickSort(more) - - less = append(less, middle...) - less = append(less, more...) - - return less +//Code snippet of user vderyagin + +package qsort + +import "math/rand" + +func QuickSort(slice []int) []int { + length := len(slice) + + if length <= 1 { + sliceCopy := make([]int, length) + copy(sliceCopy, slice) + return sliceCopy + } + + m := slice[rand.Intn(length)] + + less := make([]int, 0, length) + middle := make([]int, 0, length) + more := make([]int, 0, length) + + for _, item := range slice { + switch { + case item < m: + less = append(less, item) + case item == m: + middle = append(middle, item) + case item > m: + more = append(more, item) + } + } + + less, more = QuickSort(less), QuickSort(more) + + less = append(less, middle...) + less = append(less, more...) + + return less } \ No newline at end of file diff --git a/QuickSort.java b/QuickSort.java index 987f0db..f633b29 100644 --- a/QuickSort.java +++ b/QuickSort.java @@ -1,13 +1,20 @@ -class QuickSort -{ +/**Comments by SpaceDigi + * Please refactor this code + * Please change view for your codestyle + * Please change your codestyle(for ksklee) + * This code aren't working, it's can't be compiling + * You have two name of classes and there are equals. So, it's must not be in one file or one directory + * Please read again java naming classes, packaging, etc. + * With love, by SpaceDigi + */ + +class QuickSort { - int partition(int arr[], int low, int high) - { + int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low-1); // index of smaller element - for (int j=low; j Array to be sorted, low --> Starting index, high --> Ending index */ - void sort(int arr[], int low, int high) + public void sort(int arr[], int low, int high) { if (low < high) { @@ -69,7 +76,6 @@ public static void main(String args[]) printArray(arr); } } -======= /** * @author ksklee @@ -87,18 +93,18 @@ int partition(int arr[], int low, int high){ arr[j] = temp; } } - + int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; - + return i+1; } void sort(int arr[], int low, int high){ if (low < high){ int pi = partition(arr, low, high); - + sort(arr, low, pi-1); sort(arr, pi+1, high); } @@ -114,17 +120,17 @@ static void printArray(int arr[]){ public static void main(String[] args) { int arr[] = {90, 57, 68, 89, 11, 35}; int n = arr.length; - + System.out.println("Before:"); printArray(arr); - + QuickSort quicksort = new QuickSort(); quicksort.sort(arr, 0, n-1); - + System.out.println("After:"); printArray(arr); } - + } diff --git a/QuickSortKotlin.kt b/QuickSortKotlin.kt new file mode 100644 index 0000000..6705cb4 --- /dev/null +++ b/QuickSortKotlin.kt @@ -0,0 +1,54 @@ +/** + * Code written by SpaceDigi + * Implementation quicksort algorithm on Kotlin + * This object can be used from Kotlin and Java classes + */ +object QuickSortKotlin { + fun quickSort(lowerIndex: Int, higherIndex: Int, inputArr: IntArray) { + var i = lowerIndex + var j = higherIndex + // calculate pivot number, I am taking pivot as middle index number + val pivot = inputArr[lowerIndex + (higherIndex - lowerIndex) / 2] + // Divide into two arrays + while (i <= j) { + while (inputArr[i] < pivot) { + i++ + } + while (inputArr[j] > pivot) { + j-- + } + if (i <= j) { + exchangeNumbers(i, j,inputArr); + //move index to next position on both sides + i++ + j-- + } + } + // call quickSort() method recursively + if (lowerIndex < j) + quickSort(lowerIndex, j,inputArr) + if (i < higherIndex) + quickSort(i, higherIndex,inputArr) + } + + private fun exchangeNumbers(i:Int, j:Int, inputArr: IntArray) { + val temp = inputArr [i] + inputArr[i] = inputArr[j] + inputArr[j] = temp + } +} + +fun main(args:Array){ + System.out.println("So, the main description you can read in header of this file." + + "\nArray send to method(sorting) by reference, so method has access to array by reference to object and works with that" + + " \nWe are creating array, fill by random numbers, printing before and after sorting") + System.out.println("Before sorting") + val size = 25 + val arr = IntArray(size,{_->(Math.random()*size).toInt()}) + for(k in arr) + System.out.print("$k ") + QuickSortKotlin.quickSort(0,size-1,arr) + System.out.println("\nAfter sorting") + for(k in arr) + System.out.print("$k ") +} \ No newline at end of file diff --git a/README.md b/README.md index f408594..b0f9214 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# Quick-Sort -Add your code of Quick Sort in any language of your choice! +# Quick-Sort +Add your code of Quick Sort in any language of your choice! diff --git a/quickSort.py b/quickSort.py index 781b7af..d651ffc 100644 --- a/quickSort.py +++ b/quickSort.py @@ -1,24 +1,24 @@ -def quickSort(low, high, arr): - if(low xs - case false => { - val pivot = xs(xs.length / 2) - sort(xs filter (pivot > _)) ++ Array(pivot) ++ sort(xs filter (pivot < _)) - } - } - } - - def sort(xs : List[Int]) : List[Int] = { - xs match { - case x :: Nil => xs - case x :: ys => { - val pivot = x - sort (ys filter (pivot > _)) ::: List(pivot) ::: sort (ys filter (pivot < _)) - } - case Nil => Nil - } - } -} - -val studentScores = ('a' to 'z').map(x => (x, (Math.random() * 100).toInt)) -val score = studentScores.map(_._2) - -val sortedArray = new QuickSort().sort(score.toArray) -val sortedList = new QuickSort().sort(score.toList) - +/** + * Source : + * http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Scala + * http://blog.vogella.com/2009/11/13/quicksort-in-scala/ + * http://www.scala-lang.org/docu/files/ScalaByExample.pdf (chapter 2, page 3) + */ +class QuickSort { + + def sort(xs: Array[Int]): Array[Int] = { + xs.length < 2 match { + case true => xs + case false => { + val pivot = xs(xs.length / 2) + sort(xs filter (pivot > _)) ++ Array(pivot) ++ sort(xs filter (pivot < _)) + } + } + } + + def sort(xs : List[Int]) : List[Int] = { + xs match { + case x :: Nil => xs + case x :: ys => { + val pivot = x + sort (ys filter (pivot > _)) ::: List(pivot) ::: sort (ys filter (pivot < _)) + } + case Nil => Nil + } + } +} + +val studentScores = ('a' to 'z').map(x => (x, (Math.random() * 100).toInt)) +val score = studentScores.map(_._2) + +val sortedArray = new QuickSort().sort(score.toArray) +val sortedList = new QuickSort().sort(score.toList) + diff --git a/quickSort_2.py b/quickSort_2.py index 5845520..78f3e55 100644 --- a/quickSort_2.py +++ b/quickSort_2.py @@ -1,19 +1,19 @@ -def quickSort(x): - if len(x) == 1 or len(x) == 0: - return x - else: - pivot = x[0] - i = 0 - for j in range(len(x)-1): - if x[j+1] < pivot: - x[j+1],x[i+1] = x[i+1], x[j+1] - i += 1 - x[0],x[i] = x[i],x[0] - first_part = quickSort(x[:i]) - second_part = quickSort(x[i+1:]) - first_part.append(x[i]) - return first_part + second_part - -alist = [54,26,93,17,77,31,44,55,20] -quickSort(alist) -print(alist) +def quickSort(x): + if len(x) == 1 or len(x) == 0: + return x + else: + pivot = x[0] + i = 0 + for j in range(len(x)-1): + if x[j+1] < pivot: + x[j+1],x[i+1] = x[i+1], x[j+1] + i += 1 + x[0],x[i] = x[i],x[0] + first_part = quickSort(x[:i]) + second_part = quickSort(x[i+1:]) + first_part.append(x[i]) + return first_part + second_part + +alist = [54,26,93,17,77,31,44,55,20] +quickSort(alist) +print(alist) diff --git a/quick_sort.c b/quick_sort.c index c0f12b3..cedfff1 100644 --- a/quick_sort.c +++ b/quick_sort.c @@ -1,50 +1,50 @@ -#include -#include - -void swap(int *a,int *b) -{ - int t=*a; - *a=*b; - *b=t; -} - -int partition (int a[],int start,int end) -{ - int i=start+1,j; - int pivot=a[start]; //taking the first element as pivot; random element can also be taken - for (j=start+1; j<=end;j++)//sorting both side of pivot - { - if (a[j] +#include + +void swap(int *a,int *b) +{ + int t=*a; + *a=*b; + *b=t; +} + +int partition (int a[],int start,int end) +{ + int i=start+1,j; + int pivot=a[start]; //taking the first element as pivot; random element can also be taken + for (j=start+1; j<=end;j++)//sorting both side of pivot + { + if (a[j] quicksort [6,4,1,3,8,6,2,7,7,1337] --} - -quicksort :: [Int] -> [Int] -quicksort [] = [] -quicksort (x:xs) = smaller ++ equal ++ greater - where smaller = quicksort $ (filter (x) xs) +module QSORT where + +{- + Usage Notes: + $ ghci quick_sort.hs + *QSORT> quicksort [6,4,1,3,8,6,2,7,7,1337] +-} + +quicksort :: [Int] -> [Int] +quicksort [] = [] +quicksort (x:xs) = smaller ++ equal ++ greater + where smaller = quicksort $ (filter (x) xs) diff --git a/quick_sort.java b/quick_sort.java index 1689d48..93a05da 100644 --- a/quick_sort.java +++ b/quick_sort.java @@ -1,78 +1,78 @@ -// Java program for implementation of QuickSort -class QuickSort -{ - /* This function takes last element as pivot, - places the pivot element at its correct - position in sorted array, and places all - smaller (smaller than pivot) to left of - pivot and all greater elements to right - of pivot */ - int partition(int arr[], int low, int high) - { - int pivot = arr[high]; - int i = (low-1); // index of smaller element - for (int j=low; j Array to be sorted, - low --> Starting index, - high --> Ending index */ - void sort(int arr[], int low, int high) - { - if (low < high) - { - /* pi is partitioning index, arr[pi] is - now at right place */ - int pi = partition(arr, low, high); - - // Recursively sort elements before - // partition and after partition - sort(arr, low, pi-1); - sort(arr, pi+1, high); - } - } - - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i pivot) { - j--; - } - - if (i <= j) { - swap(items, i, j); - i++; - j--; - } - } - - return i; -} - -function quickSort(items, left, right) { - - var index; - - //if the function is called with an empty array or a single-element array, we don't do anything - if (items.length > 1) { - - left = typeof left != "number" ? 0 : left; //if the left pointer is not set we set it to the first element of the array - right = typeof right != "number" ? items.length - 1 : right; //if the right pointer is not set we select the last element of the array - - index = partition(items, left, right); //partition the array given left and right. index will be where the left pointer ended up after the partition - - if (left < index - 1) { - quickSort(items, left, index - 1); //quicksort the left side of the partition - } - - if (index < right) { - quickSort(items, index, right); //quicksort the right side of the partition - } - - } - - return items; -} - -// USAGE +function swap(items, firstIndex, secondIndex){ + var temp = items[firstIndex]; + items[firstIndex] = items[secondIndex]; + items[secondIndex] = temp; +} + +function partition(items, left, right) { + + var pivot = items[Math.floor((right + left) / 2)], //take the pivot around the middle of the array (could be ceiling function too) + i = left, + j = right; + + while (i <= j) { + + while (items[i] < pivot) { + i++; + } + + while (items[j] > pivot) { + j--; + } + + if (i <= j) { + swap(items, i, j); + i++; + j--; + } + } + + return i; +} + +function quickSort(items, left, right) { + + var index; + + //if the function is called with an empty array or a single-element array, we don't do anything + if (items.length > 1) { + + left = typeof left != "number" ? 0 : left; //if the left pointer is not set we set it to the first element of the array + right = typeof right != "number" ? items.length - 1 : right; //if the right pointer is not set we select the last element of the array + + index = partition(items, left, right); //partition the array given left and right. index will be where the left pointer ended up after the partition + + if (left < index - 1) { + quickSort(items, left, index - 1); //quicksort the left side of the partition + } + + if (index < right) { + quickSort(items, index, right); //quicksort the right side of the partition + } + + } + + return items; +} + +// USAGE //var result = quickSort(items); \ No newline at end of file