From d28ff288a37635268258bc1a8ff42c49c8d0058b Mon Sep 17 00:00:00 2001 From: ROHIT SHARMA Date: Fri, 2 Oct 2020 14:39:01 +0530 Subject: [PATCH 1/2] in place quicksort --- in-place quicksort.txt | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 in-place quicksort.txt diff --git a/in-place quicksort.txt b/in-place quicksort.txt new file mode 100644 index 0000000..d9ef8da --- /dev/null +++ b/in-place quicksort.txt @@ -0,0 +1,59 @@ +// in-place quicksort... also called Lomuto Partitioning method. +// here the last element is taken as the pivot element +// also, in the output, the array is printed after every successful partition + +#include +#include +#include +#include + +void display(int n, int *a) + { + for(int i=0;i=q) + return; + else + { + int m; + m=partition(p,q,n,a); + display(n,a); + quicksort(p,m-1,n,a); + quicksort(m+1,q,n,a); + } +} +int main(void) { + int ar_size; + scanf("%d", &ar_size); + int ar[ar_size], i; + for(i = 0; i < ar_size; i++) { + scanf("%d", &ar[i]); + } + + quicksort(0,ar_size-1,ar_size, ar); + + return 0; +} From ed13c7642ff99bf273af16cc016d94fd70895ee2 Mon Sep 17 00:00:00 2001 From: advaya08 <32246954+advaya08@users.noreply.github.com> Date: Tue, 27 Oct 2020 23:11:09 +0530 Subject: [PATCH 2/2] Leetcode problem 200 : Number of islands --- number_of_islands.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 number_of_islands.cpp diff --git a/number_of_islands.cpp b/number_of_islands.cpp new file mode 100644 index 0000000..6106d6f --- /dev/null +++ b/number_of_islands.cpp @@ -0,0 +1,40 @@ +// https://leetcode.com/problems/number-of-islands/ +// Approach to solution - DFS + +class Solution { +public: + void topol(vector>& grid, int i , int j, int &width, int &len) { + grid[i][j] = '0'; + + if(i!=0 && '0' != grid[i-1][j]) { + topol(grid, i-1, j, width, len); + } + if(j!=0 && '0' != grid[i][j-1]) { + topol(grid, i, j-1, width, len); + } + if(i!=len-1 && '0' != grid[i+1][j]) { + topol(grid, i+1, j, width, len); + } + if(j!=width-1 && '0' != grid[i][j+1]) { + topol(grid, i, j+1, width, len); + } + } + + int numIslands(vector>& grid) { + if(grid.empty()) { + return 0; + } + int count = 0, len = grid.size(), width = grid[0].size(); + //cout << len << " " << width << endl; + unordered_map visited; + for(int i = 0 ; i < len ; ++i) { + for(int j = 0 ; j < width ; ++j) { + if('0' != grid[i][j]) { + topol(grid, i, j, width, len); + ++count; + } + } + } + return count; + } +};