Binary Search
Implementation
# function for quick sort def quicksort(MyList): for i in range(len(MyList)): curr = MyList[i] j = i-1 while j >= 0 and curr < MyList[j] : MyList[j + 1] = MyList[j] MyList[j] = curr j = j - 1 # test quick sort code MyList = [1, 10, 23, 50, 4, 9, -4] print("Original List") for i in MyList: print(i, end=" ") print("\n") quicksort(MyList) print("Sorted List") for i in MyList: print(i, end=" ")
The above code will give the following output:
Original List 1 10 23 50 4 9 -4 Sorted List -4 1 4 9 10 23 50
public class MyClass { // function for quick sort static void quicksort(int Array[]) { int n = Array.length; for(int i=0; i<n; i++) { int curr = Array[i]; int j = i - 1; while(j >= 0 && curr < Array[j]) { Array[j + 1] = Array[j]; Array[j] = curr; j = j - 1; } } } // function to print array static void PrintArray(int Array[]) { int n = Array.length; for (int i=0; i<n; i++) { System.out.print(Array[i] + " "); } System.out.println(); } //test quick sort code public static void main(String[] args) { int[] MyArray = {1, 10, 23, 50, 4, 9, -4}; System.out.println("Original Array"); PrintArray(MyArray); quicksort(MyArray); System.out.println("\nSorted Array"); PrintArray(MyArray); } }
The above code will give the following output:
Original Array 1 10 23 50 4 9 -4 Sorted Array -4 1 4 9 10 23 50
#include <iostream> using namespace std; // function for quick sort static void quicksort(int Array[], int n) { for(int i=0; i<n; i++) { int curr = Array[i]; int j = i - 1; while(j >= 0 && curr < Array[j]) { Array[j + 1] = Array[j]; Array[j] = curr; j = j - 1; } } } // function to print array static void PrintArray(int Array[], int n) { for (int i=0; i<n; i++) { cout<<Array[i]<<" "; } cout<<"\n"; } // test quick sort code int main (){ int MyArray[] = {1, 10, 23, 50, 4, 9, -4}; int n = sizeof(MyArray) / sizeof(MyArray[0]); cout<<"Original Array\n"; PrintArray(MyArray, n); quicksort(MyArray, n); cout<<"\nSorted Array\n"; PrintArray(MyArray, n); return 0; }
The above code will give the following output:
Original Array 1 10 23 50 4 9 -4 Sorted Array -4 1 4 9 10 23 50
#include <stdio.h> // function for quick sort static void quicksort(int Array[], int n) { for(int i=0; i<n; i++) { int curr = Array[i]; int j = i - 1; while(j >= 0 && curr < Array[j]) { Array[j + 1] = Array[j]; Array[j] = curr; j = j - 1; } } } // function to print array static void PrintArray(int Array[], int n) { for (int i=0; i<n; i++) { printf("%i ",Array[i]); } printf("\n"); } // test quick sort code int main (){ int MyArray[] = {1, 10, 23, 50, 4, 9, -4}; int n = sizeof(MyArray) / sizeof(MyArray[0]); printf("Original Array\n"); PrintArray(MyArray, n); quicksort(MyArray, n); printf("\nSorted Array\n"); PrintArray(MyArray, n); return 0; }
The above code will give the following output:
Original Array 1 10 23 50 4 9 -4 Sorted Array -4 1 4 9 10 23 50
using System; class MyProgram { // function for quick sort static void quicksort(int[] Array) { int n = Array.Length; for(int i=0; i<n; i++) { int curr = Array[i]; int j = i - 1; while(j >= 0 && curr < Array[j]) { Array[j + 1] = Array[j]; Array[j] = curr; j = j - 1; } } } // function to print array static void PrintArray(int[] Array) { int n = Array.Length; for (int i=0; i<n; i++) { Console.Write(Array[i] + " "); } Console.Write("\n"); } // test quick sort code static void Main(string[] args) { int[] MyArray = {1, 10, 23, 50, 4, 9, -4}; Console.Write("Original Array\n"); PrintArray(MyArray); quicksort(MyArray); Console.Write("\nSorted Array\n"); PrintArray(MyArray); } }
The above code will give the following output:
Original Array 1 10 23 50 4 9 -4 Sorted Array -4 1 4 9 10 23 50