Skip to main content

Posts

Showing posts with the label sorting

Quick Sort Algorithm

  QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. Java public class Sort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 , 8 }; quickSort(arr, 0 , arr. length - 1 ); for ( int x:arr) System. out . print (x+ " " ); } public static int partition ( int arr[], int low, int high){ int pivot = arr[high]; int i = (low- 1 ); for ( int j=low; j<high; j++){ if (arr[j] <= pivot){ i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i+ 1 ]; arr[i+ 1 ] = arr[high]; arr[high] = temp; return i+ 1 ; } public static void quickSort ( ...

Insertion Sort Algorithm

The array is virtually split into a sorted and an unsorted part. Values from the unsorted part is picked and placed at the correct position in the sorted part. Complexity:- Worst-case and average-case:-  O ( n 2  ) best case:- O(n) Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class InsertionSort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 }; for ( int i= 1 ; i<arr. length ; i++){ int temp = arr[i]; int j = i - 1 ; while (j>= 0 && arr[j]>temp){ arr[j+ 1 ] = arr[j]; j--; } arr[j+ 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python def insertion_sort(arra...

Bubble Sort Algorithm

 Bubble sort is one of the simplest sorting algorithms.    It is a comparison-based sorting algorithm.  In this sorting algorithm, each pair of adjacent elements is compared and swapped if they are not in order.  Disadvantage:-   Not suitable for large data sets. Average and worst-case complexity = O(n^2) Normal code:- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class BubbleSort{ public static void main (String[] args){ int [] arr = { 3 , 2 , 5 , 4 , 7 , 9 , 1 }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j= 0 ; j<arr. length - 1 -i; j++){ if (arr[j]>arr[j+ 1 ]){ int temp = arr[j]; arr[j] = arr[j+ 1 ]; arr[j+ 1 ] = temp; } } } for ( int x:arr) System. out . print (x+ " " ); } } Optim...

Program to sort the first half of an array in ascending and second half in descending order

Ex:-  [2, 4, 3, 10, 5, 8] [2, 4, 3]  and [10, 5, 8] ====>  [2, 3, 4] ascending                                                              [10, 8, 5] descending [ 2, 3, 4, 10, 8, 5] is the required answer.  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 public class ArraySort{ public static void main (String[] args){ int [] arr = { 2 , 4 , 3 , 10 , 5 , 8 }; //sorting first half in ascending order for ( int i= 0 ; i<(arr. length / 2 ); i++){ for ( int j=i+ 1 ; j<(arr. length / 2 ); j++){ if (arr[i]>arr[j]){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } ...

Problem Statement 2

After Sorting odd and even indexed position element of the Array separately. Find the Sum of 2nd smallest element from both arrays. Take the size of the array as input from the user. Input array elements  Ex:-    int[ ] arr = { 2, 4, 9, 0, 3, 5, 7, 1 };                            oddindexedarray = { 2, 9, 3, 7 }       evenindexedarray = { 4, 0, 5, 1 }           After sorting :-                           oddindexedarray = { 2, 3, 7, 9 }        evenindexedarray = { 0, 1, 4, 5 }          hence, answer should be  3 + 1 = 4 Note:- Array size should be not less than 4. 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 impo...

Find the largest and smallest number in an unsorted integer array or list

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class LargestSmallest{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 , 3 , 4 , 1 }; int large = Integer. MIN_VALUE ; int small = Integer. MAX_VALUE ; for ( int i= 0 ; i<arr. length ; i++){ if (arr[i]<small) small = arr[i]; else if (arr[i]>large) large = arr[i]; } System. out . println ( "largest:" +large); System. out . println ( "smallest:" +small); } }  Java 2nd Approach 1 2 3 4 5 6 7 8 9 import java.util.Arrays; public class LargestSmallest2{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 , ...