Skip to main content

Posts

Showing posts with the label Searching and Sorting

Count Small Java Sorting

 Question:  Input: A = [2, 3, 0] B = [5, 1] Output: [1, 1, 0] package learningJava ; public class countSmall { // TODO Auto-generated method stub /*You are given two arrays of integers. Let's call the first array A and the second array B. A finds the number of elements in array B that are smaller than or equal to that element for every array element. Example: Input: A = [2, 3, 0] B = [5, 1] 1) where first array's 2 is greater than 1 then count 1 and print 2) int 3 is grater than 1 then count 1 and print 1 3) int 0 is not grater than any element's of 2nd array then print zero times Output: [1, 1, 0]*/ public static void countSmallElement ( int arr1 [] , int arr2 [] , int ans []) { for ( int i = 0 ; i < arr1 . length ; i ++ ) { for ( int j = 0 ; j < arr2 . length ; j ++ ) { if ( arr1 [ i ] > arr2 [ j ]) { ans [ i ] ++; ...

Find Pair With Smallest Difference Searching and Sorting .

Question:  Given two unsorted arrays of non-negative integers, 'arr1' and 'arr2' of size 'N' and 'M', respectively. Your task is to find the pair of elements (one from each array), such that their absolute (non-negative) difference is the smallest, and return the difference. Example : N = 3, arr1 = [10, 20, 30] M = 2, arr2 = [17, 15]  The smallest difference pair is (20, 17) with an absolute difference of 3. So, the answer is 3.   package learningJava ; public class pairSmallest { public static int pairDifference ( int arr1 [] , int arr2 [] , int min ) { for ( int i = 0 ; i < arr1 . length ; i ++ ) { for ( int j = 0 ; j < arr2 . length ; j ++ ) { //iterate from 0th index to arr2 size int difference = Math . abs ( arr1 [ i ] - arr2 [ j ]) ; //difference between 2 arrays //Math.abs is a function for get absolute value instead negative value if ( difference < min ) ...

Sorting Algorithum In Array Java

Question :  What is Sorting in Java ? Ans: Sorting in Java refers to the process of arranging elements in a specific order, often in ascending (smallest to largest) or descending (largest to smallest) order. Java provides various ways to sort data structures like arrays, collections, and lists.  Question: Why use Sorting ? Ans:  Sorting algorithms are used in computer science and programming for a variety of reasons: 1. **Data Retrieval and Searching**: Sorted data is much easier and faster to search. Algorithms like binary search can be used to find elements in a sorted list efficiently, reducing the time complexity from O(n) to O(log n). 2. **Data Presentation**: Sorted data is often presented to users in a more readable and meaningful way. For example, a sorted list of names or numbers is more user-friendly than an unsorted one. 3. **Data Analysis**: In data analysis and statistics, sorting can help identify trends and patterns more easily. It can simplify tasks like ...

Binary Search In Java || Searching and Sorting Java

  What is Binary Search ? **Binary Search (द्विआधारी खोज)**: Binary Search ek prashikshan (searching) technique hai jo data ko tezi se khojne me madadgar hoti hai. Isme data ko do bhago me batakar dhunda jata hai, aur har bar ek adha data set ko chhodkar dusre adhe ko chhota banaya jata hai, jisse tezi se manchit samuday (sorted array) me kuch vishesh data ko khoja ja sake. Yadi aapko kisi data set me kisi vishesh item ko dhoondhna hai, to binary search us data set ko do hisson me vibhajit karega aur dekhega ki dhoondhne wala item kis bhag me ho sakta hai. Phir is prakriya ko pratek bar lagataar dubara karta hai aur data set ko adhikansh roop se ghata deta hai, jab tak aapko aapka vishesh item mil jaye ya fir yah spasht ho jaye ki item data set me maujood nahi hai. Binary Search ki khasiyat yah hai ki yah data set me tezi se khojne ki kshamata rakhta hai kyunki har bar dui sankhyaon ke bich ka adha data set ko chhod deta hai, jisse dhoondhne ke liye keval ek chota hissa bachta hai....