Selenium Automation | Java Sort Algorithms 21-25

As soon as you write Selenium Automation in your resume, interviewer is bound to give you a sheet of paper & ask write a Java program. Though Selenium doesn’t require extensive Java knowledge but still there is a set of basic Java programs that you should be able to write on-the-spot. Continuing on our Java interview series for Selenium Automation, this article covers five of the common Java Sort programs frequently asked in the basic technical interview. The questions are easy, but don’t forget to mark these java programs in your to do list before attending any entry-level technical interview for Selenium automation testing.

Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

packageJava;

publicclassBubbleSort{

// Complexity = O(n2) | Best case = O(n)

staticinttemp;

publicstaticvoidbubble_srt(intarray[]){

intn=array.length;

intk;

for(intm=n;m&gt;=0;m--){

for(inti=0;i&lt;n-1;i++){k=i+1;if(array[i]&gt;array[k]){

temp=array[i];

array[i]=array[k];

array[k]=temp;

}

}

printNumbers(array);

}

}

privatestaticvoidprintNumbers(int[]input){

for(inti=0;i&lt;input.length;i++){

System.out.print(input[i]+", ");

}

System.out.println("\n");

}

publicstaticvoidmain(String[]args){

int[]input={4,2,9,6,23,12,34,0,1};

bubble_srt(input);

}

}

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms, such as merge sort with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

Insertion Sort

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

The Logic: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

packageJava;

publicclassInsertionSort{

// Complexity | Best O(n) | Worst O(n2) | Average O(n2)

publicstaticint[]IS(int[]input){

inttemp;

for(inti=1;i&lt;input.length;i++){for(intj=i;j&gt;0;j--){

if(input[j]&lt;input[j-1]){

temp=input[j];

input[j]=input[j-1];

input[j-1]=temp;

}

}

}

returninput;

}

publicstaticvoidmain(Stringa[]){

int[]arr1={10,34,2,56,7,67,88,42};

int[]arr2=IS(arr1);

for(inti:arr2){

System.out.print(i);

System.out.print(", ");

}

}

}

The best case input is an array that is already sorted. In this case insertion sort has a linear running time (i.e., O(n)). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array.

The simplest worst case input is an array sorted in reverse order. The set of all worst case inputs consists of all arrays where each element is the smallest or second-smallest of the elements before it. In these cases every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., O(n2)).

Selection Sort

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

packageJava;

publicclassSelectionSort{

// Complexity = n(n-1)/2 | O(n2)

publicstaticint[]SS(int[]a){

for(inti=0;i&lt;a.length-1;i++){

intindex=i;

for(intj=i+1;j&lt;a.length;j++)

if(a[j]&lt;a[index])

index=j;

inttemp=a[index];

a[index]=a[i];

a[i]=temp;

}

returna;

}

publicstaticvoidmain(Stringa[]){

int[]arr1={10,34,2,56,7,67,88,42};

int[]arr2=SS(arr1);

for(inti:arr2){

System.out.print(i);

System.out.print(", ");

}

}

}

Merge Sort

Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

packageJava;

publicclassMergeSort{

privatestaticint[]array;

privatestaticint[]tempMergArr;

privatestaticintlength;

publicstaticvoidsort(intinputArr[]){

doMergeSort(0,length-1);

}

privatestaticvoiddoMergeSort(intlowerIndex,inthigherIndex){

if(lowerIndex&lt;higherIndex){

intmiddle=lowerIndex+(higherIndex-lowerIndex)/2;

// Below step sorts the left side of the array

doMergeSort(lowerIndex,middle);

// Below step sorts the right side of the array

doMergeSort(middle+1,higherIndex);

// Now merge both sides

mergeParts(lowerIndex,middle,higherIndex);

}

}

privatestaticvoidmergeParts(intlowerIndex,intmiddle,inthigherIndex){

for(inti=lowerIndex;i&lt;=higherIndex;i++){

tempMergArr[i]=array[i];

}

inti=lowerIndex;

intj=middle+1;

intk=lowerIndex;

while(i&lt;=middle&amp;&amp;j&lt;=higherIndex){

if(tempMergArr[i]&lt;=tempMergArr[j]){

array[k]=tempMergArr[i];

i++;

}else{

array[k]=tempMergArr[j];

j++;

}

k++;

}

while(i&lt;=middle){

array[k]=tempMergArr[i];

k++;

i++;

}

}

publicstaticvoidmain(Stringa[]){

int[]inputArr={45,23,11,89,77,98,4,28,65,43};

sort(inputArr);

for(inti:inputArr){

System.out.print(i);

System.out.print(" ");

}

}

}

Quick Sort

Quicksort is a comparison sort, meaning that it can sort items of any type for which a “less-than” relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. It is very similar to selection sort, except that it does not always choose worst-case partition.

Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.

Related Articles

Sometimes systems crush. And when they crush, they become headline news. The past few years have seen a sharp increase in the number and severity of software glitches at major companies. These newsworthy software “fails” have a negative impact on customer satisfaction & public reputation. The aftermath of each new failure is frequently projected across the internet, and can follow businesses around for years. The results of a phenomenally simple software error can affect the life of millions of people in the most negative way. Let’s take a look at some of the top Software glitches of 2017 to serve as a reminder of just how important Software testing is.

Any technology or tool is worthless unless it is being used by ‘some’ organization somewhere. It all starts from organizations adopting the new technology or a tool and then it gets popular slowly. In that sense QA Job Descriptions are a great source of current technology, i.e. practical tech. being used by IT organizations. Be it Selenium, Protractor, Appium, API tools, Big Data Testing, etc. Everything is embedded in the QA Job descriptions, you just need to mine some data 😉 But don’t worry. Continuing on our “JD Talks” series – we mine hundreds of QA Job descriptions to come up with latest tools, technology, languages and concepts. Let’s see what the third set of JDs talk about…

First of all, the question ‘What’s the average salary of Software Test Engineer’ is like asking ‘What’s the average house rent in Bengaluru, India?’ You tell me…Can’t? Yeah! You need some more filters to arrive at the rent – the house type (whether it’s a 1BHK/2BHK/Villa/etc.), the locality, furnishing status, society amenities, etc. In the same way, the pay scale for Software Testing domain (in fact for any technology) varies depending on multiple factors.

About STS

Software Testing Studio is an attempt to share some incredible knowledge from industry leaders & experts, which should be helpful for anybody to start his/her career in ‘Software Testing’ or to progress it further. Apart from the technical nitty-gritties, one can also find some intellectual posts by industry experts sharing their Wisdom.