Beruflich Dokumente
Kultur Dokumente
Insertion sort is a sorting algorithm in Following are the steps involved in insertion
which the elements are transferred one at a sort:
time to the right position. In other words,
1. insertionSort(array)
an insertion sort helps in building the
2. mark first element as sorted
final sorted list, one item at a time, with the
3. for each unsorted element X
movement of higher-ranked elements.
4. 'extract' the element X
import java.util.Arrays; 5. for j <- lastSortedIndex down to 0
6. if current element j > X
class InsertionSort { 7. move sorted element to the right by
void insertionSort(int array[]) { 1
int size = array.length; 8. break loop and insert X here
for (int step = 1; step < size; step++) { 9. end insertionSort
int key = array[step];
int j = step - 1;
while (j >= 0 && key < array[j]) {
// For descending order, change
key<array[j] to key>array[j].
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
Selection sort is conceptually the most Following are the steps involved in selection
sort(for sorting a given array in ascending
simplest sorting algorithm. This algorithm will
order):
first find the smallest element in the array
and swap it with the element in 1. Starting from the first element, we
the first position, then it will find the second search the smallest element in the
array, and replace it with the element
smallest element and swap it with the
in the first position.
element in the second position, and it will
2. We then move on to the second
keep on doing this until the entire array is
position, and look for smallest element
sorted. It is called selection sort because it
present in the subarray, starting from
repeatedly selects the next-smallest
index 1, till the last index.
element and swaps it into the right place.
3. We replace the element at
the second position in the original
array, or we can say at the first
# include <stdio.h>
position in the subarray, with the
// function to swap elements at the given second smallest element.
index values
4. This is repeated, until the array is
void swap(int arr[], int firstIndex, int
secondIndex) completely sorted.
{
int temp;
temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
// function to look for smallest element in the
given subarray
int indexOfMinimum(int arr[], int startIndex,
int n)
{
int minValue = arr[startIndex];
int minIndex = startIndex;
int main()
{
int arr[] = {46, 52, 21, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
import java.util.*;
Quicksort is a divide and conquer algorithm. It first divides a large list into
two smaller sub-lists and then recursively sort the two sub-lists. If we want
to sort an array without any extra space, quicksort is a good option. On
average, time complexity is O(n log(n)).
Quicksort is a popular sorting algorithm that is often faster in practice
compared to other sorting algorithms. It utilizes a divide-and-conquer
strategy to quickly sort data items by dividing a large array into two smaller
arrays.
int max=j;
for(int K=j+1;K<array.length;K++)
if(array[K]<array[max])max=K;
int temp=array[j];
array[j]=array[max];
array[max]=temp;
}
}
public static void main(String[] args){
int[] values ={-11,-8,9,1,3,8,-10,-15};
System.out.println("Original Array");
for(int val:values)
System.out.print(val+",");
quickSort(values);
System.out.println();
System.out.println("Sorted Array:");
for(int val: values)
System.out.print(val+",");
System.out.println();
}
}
Module 3
Search operation
class Main
{
// Function to implement
// search operation
static int findElement(int arr[], int n,
int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {12, 34, 10, 6, 40};
int n = arr.length;
if (position == - 1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
Output :
Element found at position 5
Deletion operation
class Main
{
// function to search a key to
// be deleted
static int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
if (pos == -1)
{
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
public static void main(String args[])
{
int i;
int arr[] = {10, 50, 30, 40, 20};
int n = arr.length;
int key = 30;
n = deleteElement(arr, n, key);
System.out.println("\n\nArray after deletion");
for (i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
}
Output :
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20
Insertion operation
class Main
{
// Function to insert a given key in
// the array. This function returns n+1
// if insertion is successful, else n.
static int insertSorted(int arr[], int n,
int key,
int capacity)
{
arr[n] = key;
return (n + 1);
}
// Driver Code
public static void main (String[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
// Inserting key
n = insertSorted(arr, n, key, capacity);
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26