Bubble sort.
Approach: Swap Adjacent elements from an array a[j] > a[j+1] then swap
Approach: Swap Adjacent elements from an array a[j] > a[j+1] then swap
Step 1: Find minimum element in array & swap with beginning element Step 2: So now swap min elements from inner loop in the array. So we have taken `minIndex` which assigned from 0th element i array till last & compare it with i+1 element
[Question]: In given array find difference between lowest & highest occurrence elements . For Example: Array : [5,4,5,5,6,9,5,6] O/P = 2. because 5 appears 4 times & element 6 is 2 times. difference is 2.
[Question]: Count frequency of each element in a given array .For example: Given Array [5,4,5,5,6,9,5,6]. Output is [4: 1, 6: 2, 9: 1, 5: 4] Which indicates 4 is 1 time, 6 is 2 times and so on…#Approach 1: – Use HashMap /Dictionary . Because dictionary key are unique so we can use of that …
[Question]: Check if given number is prime ? Prime numbers are divisible by self & 1 onlyExample prime number : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 #Approach 1: if the number is perfectly divisible by 2,3,5 Then it’s not prime. Approach #2: Take a …
[Question] Print all divisors of a given number For example divisors of 36 are = 1,2,3,4,5,9,12,18,36#Approach: We can find square root of a given number & check if its perfectly divisible sqrt numbers
[Question]: Check if given number is Armstrong. for example : 371 = 3 pow 3 + 7 pow 3 + 1 pow 3 = 371 The count of digits to power of each digits & sum is equivalent to self number. (pow id power )
[Question]: There is two number find the Greatest common divisor example 3,6 – GCD is 3Better Approach using Euclidean algorithm Here we used recursion which swap the params with second argument as modulo divisor. If we do dry run for above code snippet Case 1: For 3,11 a & b values in loop will be …
Below are the example code for various type of patterns in swift language. Reference Link: Take You Forward
[Question]: In a given array sort all 0s 1s & 2s .For example: I/P {0, 1, 2, 0, 1, 2} o/p:: {0, 0, 1, 1, 2, 2}Solution:- We can use three pointers low, mid & high. Where low & mid is start index(i.e. 0) & high is arrayLength – 1. In a while loop we …