Skip to content

Commit

Permalink
no summary
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyCoding8 committed Oct 7, 2023
1 parent 90f3677 commit 43a2511
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 75 deletions.
44 changes: 24 additions & 20 deletions data_structures/Arrays/Java/CountNegativeNumbersIn2DArray.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
class Solution {
public int countNegatives(int[][] grid) {
int rows = grid.length;
int columns = grid[0].length;
int rp = 0;
int cp = columns -1;
int count=0;
while(cp>=0 && rp<rows)
{
if(grid[rp][cp]<0)
{
count+=(rows-rp);
cp--;
import java.util.Scanner;
import java.util.*;

public class countNegativeNumbersIn2dArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int n = sc.nextInt();
System.out.println("Enter the number of columns:");
int m = sc.nextInt();
int[][] arr = new int[n][m];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
else
{
rp++;
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] < 0) {
count++;
}
}

}
return count;

System.out.println("The number of negative numbers in the array is: " + count);
}
}
}
51 changes: 28 additions & 23 deletions search/binary_search/C/binarySearch.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

int binarySearch(int arr[], int start, int end, int x) {
printf("Enter number of elements\n");
scanf("%d", &n);

if (start <= end) {
printf("Enter %d integers\n", n);

int mid = start + (end - start) / 2;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

if (arr[mid] == x)
return mid;
printf("Enter value to find\n");
scanf("%d", &search);

if (arr[mid] > x)
return binarySearch(arr, start, mid - 1, x);
first = 0;
last = n - 1;
middle = (first+last)/2;

return binarySearch(arr, mid + 1, end, x);
}
return -1;
}
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

int main()
{
int arr[] = { 2, 3, 7, 8, 78, 99, 102, 5555 };
int x = 9;
int result = binarySearch(arr, 0, sizeof(arr) / sizeof(arr[0]), x);

if (result == -1) {
printf("Element not present");
}
else {
printf("Element found at index %d", result);
}
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
63 changes: 31 additions & 32 deletions sort/counting_sort/Java/CountSort.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
public class countingSort {
static void CountSort(int a[],int n){
int max=a[0];
for (int i = 0; i <n; i++) {
if(a[i]>max){
max=a[i];
}
}
int[] elements=new int[max+1];
for (int i = 0; i <n; i++) {
elements[a[i]]++;
}

public class CountSort {

private int getMax(int [] A) {
int max = A[0];
for(int i = 0 ; i < A.length ; i ++)
if(max< A[i])
max = A[i];
return max;
}

private void getFreq(int [] A , int [] freq) {
for(int i = 0 ; i < A.length ; i ++)
freq[A[i]] += 1 ;
}

public void sort(int [] A) {
int [] freq = new int [getMax(A)+1];
getFreq(A, freq);
for(int i = 0 , j = 0 ; i < A.length ; i ++) {
while(freq[j] == 0)
j++;
A[i] = j ;
freq[j] -= 1;
}
}

public static void main (String [] args) {
int nosrt[] = {4,3,10,8,1,16,17,1,12,11,20};
new CountSort().sort(nosrt);
for(int i = 0 ; i < nosrt.length ; i ++)
System.out.println(nosrt[i]);
}
for (int i = 1; i <= max; i++) {
elements[i]+=elements[i-1];
}
int[] output=new int[max];
for (int i = n-1; i>=0; i--) {
output[--elements[a[i]]]=a[i];
}
for (int i = 0; i < n; i++) {
a[i]=output[i];
}

}
public static void main(String[] args) {
int[] a={1,6,4,2,7,5,3,8,5,7,4,6,2,1,5,7,4,7,4,7,5,67};
CountSort(a, a.length);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}

0 comments on commit 43a2511

Please sign in to comment.