diff --git a/Searching/LinearSearch.C b/Searching/LinearSearch.C new file mode 100644 index 0000000..811fcde --- /dev/null +++ b/Searching/LinearSearch.C @@ -0,0 +1,22 @@ +#include + +int search(int arr[], int n, int x) +{ + int i; + for (i = 0; i < n; i++) + if (arr[i] == x) + return i; + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = search(arr, n, x); + (result == -1) ? printf("Element is not present in array") + : printf("Element is present at index %d", + result); + return 0; +} diff --git a/Sorting/quicksort/java/quicksort.java b/Sorting/quicksort/java/quicksort.java new file mode 100644 index 0000000..fe995de --- /dev/null +++ b/Sorting/quicksort/java/quicksort.java @@ -0,0 +1,76 @@ +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