Popular Posts

이은한. Powered by Blogger.

레이블이 Algorithm Study인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Algorithm Study인 게시물을 표시합니다. 모든 게시물 표시

2022년 3월 4일 금요일

what is Quick Sort


Quick Sort

Definition

one of sort algorithms

  • The most important sort algorithm
  • Use this algorithm anywhere with any languages
  • The best way to avoid worst time complexity is pick pivot randomly.
  • if program most not have O(n2)O(n^2), need to use other sorting algorithm.

Technique

Decrease and Conquer

Algorithm steps

Recusively loop based on Lomuto.

  1. pick pivot the most right element.
  2. left side will be smaller than pivot value and right side will be bigger than pivot.
  3. let's say most left value is selected index and comaparing index without pivot.
  4. if comaparing index value is smaller than pivot, swap with selected index and increase the selected index.
  5. if comaparing index value is bigger than or equal pivot, increase the comaparing index.
  6. when the comaparing index is equal to pivot index, swap pivot and the selected index.
  7. Recusively loop step 1~6.






how to pick pivot

  1. Lomuto

    • pivot : the most right element.
    • starting selected index (i) : 0
    • starting comaparing index (j) : 0
  2. Hoare

    • pivot : the most left element.
    • starting selected index (i) : 1
    • starting comaparing index (j) : the most right element.
  3. Randomly Choose

Java code - Lomuto


   public static void quickSort(int[] input) {
        quickSortRecur(input, 0, input.length - 1);
    }


    public static void quickSortRecur(int[] input, int left, int right) {

        if (left >= right) {
            return;
        }

        int pivotPos = partition(input, left, right);

        quickSortRecur(input, left, pivotPos - 1);
        quickSortRecur(input, pivotPos + 1, right);

    }

    public static void swap(int[] input, int a, int b) {
        int temp = input[a];
        input[a] = input[b];
        input[b] = temp;

    }

    public static int partition(int[] input, int left, int right) {
        int pivot = input[right];

        int i = (left - 1);
        for (int j = left; j < right; ++j) {
            if (input[j] < pivot) {
                ++i;
                swap(input, i, j);
            }
        }
        swap(input, (i + 1), right);
        return i + 1;
    }

Avg. time complexity

O(nlogn)O(n\, log\, n)

Worst time complexity

O(n2)O(n^2)

space complexity

O(logn)O(log\, n)

stability

no

How to calculate

numbers of loop

  • base recursion: O(n)O(n)
  • If separated evenly : O(logn)O(log\,n)
  • If separated not evenly : O(n)O(n)

Time Complexity

  • If separated evenly : O(nlogn)=O(nlogn) O(n \cdot log\,n)=O(n\,log\,n)
  • If separated not evenly (the worst case) : O(nn)=O(n2)O(n \cdot n)=O(n^2)

2022년 3월 3일 목요일

what is Merge Sort


Merge Sort

Definition

one way of sorting algorithms by Decrease and Conquer.

Algorithm steps

Recusively loop

  1. Recusively loop left side and right side until the input array cannot separated
  2. every separated parts only one element is in there. This is sorted.
  3. merge arrays with sort.


Java code


   public static void mergeSort(int[] input) {
        mergeSortRecurr(input, 0, input.length - 1);
    }

    public static void mergeSortRecurr(int[] input, int left, int right) {
        if (left < right) {
            int midPos = left + (right - left) / 2;

            mergeSortRecurr(input, left, midPos);
            mergeSortRecurr(input, midPos + 1, right);
            merge(input, left, right, midPos);
        }
    }

    public static void merge(int[] input, int left, int right, int midPos) {
        //mid pos does not check unlike the left and right
        int temp1 = midPos - left + 1;
        int temp2 = right - midPos;

        //temp array and copy
        int[] L = new int[temp1];
        int[] R = new int[temp2];
        for (int i = 0; i < temp1; ++i)
            L[i] = input[left + i];
        for (int j = 0; j < temp2; ++j)
            R[j] = input[midPos + 1 + j];


        int i = 0;
        int j = 0;
        int k = left;

        while (i < temp1 && j < temp2) {
            if (L[i] <= R[j]) {
                input[k] = L[i];
                ++i;
            } else {
                input[k] = R[j];
                ++j;
            }
            ++k;
        }

        while (i < temp1) {
            input[k] = L[i];
            ++i;
            ++k;
        }

        while (j < temp2) {
            input[k] = R[j];
            ++j;
            ++k;
        }
    }

Avg. time complexity

O(nlogn)O(n\, log\, n)

Worst time complexity

O(nlogn)O(n\, log\, n)

space complexity

O(n)O(n)

stability

yes

How to calculate

numbers of loop

  • base recursion for merge: O(n)O(n)
  • separated evenly : O(logn)O(log\,n)

Time Complexity

O(n)O(logn)=O(nlogn)O(n) \cdot O(log\, n)=O(n\, log\, n)

2022년 3월 1일 화요일

what is Insertion Sort algorithm


Insertion Sort

Definition

one way of sorting algorithms by using brute force.
check every possible ways to sort the list.

Algorithm steps

  1. There are two position. one is for moving the other one is for checking
  2. When you check two values, smaller go to left side
  3. These two positions checking started from original position.
  4. set moving position is original position and checking position is original position-1
  5. These two positions checking getting smaller untill the checking position reached to 0
  6. Repeat 1~6 until all of them sorted.




Java code

    private static int[] insertSort(int[] input) {
        if (input.length < 2) {
            return input;
        }

        for (int originalPosition = 1; originalPosition < input.length; ++originalPosition) {
            int movingPostion = originalPosition;
            int checkingPostion = originalPosition - 1;

            while (checkingPostion > 0) {
                if (input[checkingPostion] > input[movingPostion]) {
                    swap(input, checkingPostion, movingPostion);
                } else {
                    break;
                }
                --checkingPostion;
                --movingPostion;
            }
        }
        return input;
    }

    private static void swap(int[] input, int pos1, int pos2) {
        int temp = input[pos2];
        input[pos2] = input[pos1];
        input[pos1] = temp;
    }

Avg. time complexity

O(n2)O(n^2)

Worst time complexity

O(n2)O(n^2)

space complexity

O(1)O(1)

stability

no

2022년 2월 25일 금요일

what is Selection Sort algorithm


Selection Sort Algorithm

Definition

one way of sorting algorithms by using brute force.
check every possible ways to sort the list.

Algorithm steps

  1. find smallest number in the list
  2. swap the smallest number and first place
  3. the first element is sorted
  4. find smallest number in the list except the sorted element.
  5. swap the smallest number and second place
  6. Repeat 1~5 until all of them sorted.



Avg. time complexity

O(n2)O(n^2)

Worst time complexity

O(n2)O(n^2)

space complexity

O(1)O(1)

stability

no

Java code

    public static int[] selectionSort(int[] input) {
        for (int i = 0; i < input.length - 1; ++i) {
            for (int j = input.length - 1; j > i; --j) {
                if (input[j] < input[i]) {
                    int temp = input[i];
                    input[i] = input[j];
                    input[j] = temp;
                }
            }
        }
        return input;
    }

How to calculate

numbers of loop

n1n-1

The most visited place of the list

n1n-1 (the last place)

The least visited place of the list

11 (the first place)

Avg. visited numbers of the list

(The most visited place of the list+The least visited place of the list)/2 =(n1+1)2=n2= \frac{(n-1+1)}{2} = \frac{n}{2}

Polynomial Time

numbers of loop*Avg. visited numbers of the list =(n1)n2=(n-1)\cdot \frac{n}{2}

Time Complexity

O(Polynomial  Time)=O((n1)n2)=O((n22n2))=O(n2)O(Polynomial\; Time)=O((n-1)\cdot \frac{n}{2})=O((\frac{n^2}{2}-\frac{n}{2}))=O(n^2)

2022년 2월 24일 목요일

what is Bubble Sort algorithm


Bubble Sort Algorithm

Definition

one way of sorting algorithms by using brute force.
check every possible ways to sort the list.

Algorithm steps

  1. compare two numbers
  2. smaller number will be left and bigger number will be in right side.
  3. compare over and over again until the end of the list
  4. Now, biggest number will stay in most right side. This number is sorted
  5. Start over from first number except sorted place.
  6. Repeat 1~5 until all of them sorted.



Avg. time complexity

O(n2)O(n^2)

Worst time complexity

O(n2)O(n^2)

space complexity

O(1)O(1)

stability

yes

Java code

    public static int[] bubbleSort(int[] input) {
        for (int i = 0; i < input.length - 1; ++i) {
            for (int j = 0; j < input.length - i - 1; ++j) {
                if (input[j] > input[j + 1]) {
                    int temp = input[j];
                    input[j] = input[j + 1];
                    input[j + 1] = temp;
                }
            }
        }
        return input;
    }

How to calculate

numbers of loop

n1n-1

The most visited place of the list

n1n-1 (the first place)

The least visited place of the list

11 (the last place)

Avg. visited numbers of the list

(The most visited place of the list+The least visited place of the list)/2 =(n1+1)2=n2= \frac{(n-1+1)}{2} = \frac{n}{2}

Polynomial Time

numbers of loop*Avg. visited numbers of the list =(n1)n2=(n-1)\cdot \frac{n}{2}

Time Complexity

O(Polynomial  Time)=O((n1)n2)=O(n22n2)=>O(n2)O(Polynomial\; Time)=O((n-1)\cdot \frac{n}{2})=O(\frac{n^2}{2}-\frac{n}{2})=>O(n^2)

2022년 2월 22일 화요일

what is Heap Sort algorithm


Heap Sort Algorithm

Definition

one way of sorting algorithms by using binary tree data structure

Algorithm steps

insert data into binary tree and print out from the tree

  1. insert data into binary tree
  2. The data sorted as binary tree
  3. print

Avg.

O(nlogn)O(n\, log\, n)

Worst time complexity

O(nlogn)O(n\, log\, n)

space complexity

O(1)O(1)

stability

no

insert time complexity

O(logn)O(log\, n)

O(n)O(n)

Java code


    public static void heapSort(int arr[]) {
        int n = arr.length;

        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--)
            heapTree(arr, n, i);

        // One by one extract an element from heap
        for (int i = n - 1; i > 0; i--) {
            // Move current root to end
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            // call max heapify on the reduced heap
            heapTree(arr, i, 0);
        }
    }

    // To heapify a subtree rooted with node i which is
    // an index in arr[]. n is size of heap
    public static void heapTree(int arr[], int n, int i) {
        int largest = i; // Initialize largest as root
        int l = 2 * i + 1; // left = 2*i + 1
        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest])
            largest = l;

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest])
            largest = r;

        // If largest is not root
        if (largest != i) {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;

            // Recursively heapify the affected sub-tree
            heapTree(arr, n, largest);
        }
    }

*this code is from https://www.geeksforgeeks.org/heap-sort/

what is Brute Force Algorithm


Brute Force Algorithm

Definition

check every possible ways to find answer.

Points

  • No efficiency
  • most intuitive way to solving problems

Example

import java.util.NoSuchElementException;

public class Main {
    public static void main(String[] args) {

        int[] inputList = {1, 9, 44, 55, 88};
        int value = 55;

        System.out.println(findNumIndexArr(inputList, value));

    }
    public static int findNumIndexArr(int[] input, int value) {
        for (int i = 0; i < input.length; ++i) {
            if (input[i] == value) {
                return i;
            }
        }
        throw new NoSuchElementException();
    }
}

2022년 2월 21일 월요일

what is Binary Search Algorithm


Binary Search Algorithm

Definition

check middle of list first. if the number is smaller than what you are looking for, check right side only and doing this continuously. It will decrease search list in half.

  • pre-condition : the list sorted
  • pre-condition : when new data inserted, need to sort
  • Since it need to sort when new data inserted, it can be very slow if there are a lot of insertion
  • divide-and-conquer technique

Time complexity

O(log  n)O(log\; n)

Example

import java.util.NoSuchElementException;

public class Main {

    public static void main(String[] args) {

        int[] inputList = {1, 9, 44, 55, 88};
        int value = 55;

        System.out.println(getIndexValueArr(inputList, value));

    }

    private static int getIndexValueArr(int[] input, int value) {
        return binarySearchRecur(input, 0, input.length - 1, value);

    }

    private static int binarySearchRecur(int[] input,
                                         int mostLeft,
                                         int mostRight,
                                         int value) {
        if (mostRight < mostLeft) { /*value not found*/
            throw new NoSuchElementException();
        }

        int mid = mostLeft + ((mostRight - mostLeft) / 2);

        if (input[mid] == value) { /*found the value*/
            return mid;
        } else if (input[mid] > value) { /*value is left side*/
            return binarySearchRecur(input, mostLeft, mid - 1, value);
        } else { /*value is right side*/
            return binarySearchRecur(input, mid + 1, mostRight, value);
        }
    }
}

Explanation

step 1

step 2

step 3

step 4

step 5

step 6

step 7

2022년 2월 20일 일요일

what is Linear Search Algorithm


Linear Search Algorithm

Definition

How to find certain element in the list.
check all elements of the list one by one to search one element.

Time complexity

O(n)

Example

import java.util.NoSuchElementException;

public class Main {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 2, 2, 5};
        System.out.println(getIndexValueArr(nums, 5));
    }

    public static int getIndexValueArr(int[] input, int value) {
        for (int i = 0; i < input.length; ++i) {
            if (input[i] == value) {
                return i;
            }
        }
        throw new NoSuchElementException();
    }
}

Explanation

step 1

step 2

step 3

step 4

step 5

2022년 2월 17일 목요일

what is Hash Crash


Hash Crash or Hash Collision

Definition

one of the hash function's problems .

If the input value is different, the output value should be different.
But, the output is same.

  • less hash crush is better
  • No hash crush is almost impossible. We calls that "perfect hash function"
  • If you limited insert value a lot, you may create the perfect hash function

Example of Hash Crash

insert "A" --Hash Function--> return "3"
insert "B" --Hash Function--> return "1"
insert "C" --Hash Function--> return "3"

inserted values are different, but the returned values are same


check this- what is Hashing or Hash Algorithm

what is Hashing or Hash Algorithm


Hash Algorithm

Definition

1. Hashing or Hash function

one of mathematical function that converts an input of arbitrary length into an encrypted output of a fixed length.

Example of Hash function

insert "A" --Hash Function--> return "123"
insert "ABG" --Hash Function--> return "778"
insert "HEU" --Hash Function--> return "998"

2. Hash Data Structure

Programmers used hash function to create many data structures.

Examples of Java Hash Data Structures

  • Hash set
  • LinkedHashSet
  • Hash table
  • Hash map
  • LinkedHashMap
  • TreeSet
  • ConcurretHashMap

3. Hash Algorithm

Programmers used the hash data structures to solve problems.
The way to solving problems is Hash algorithm

Characteristics

  • Hash Data Structures need more memories than total list n
  • The worst case, Hash algorithm can be slow because of hash collision

Properties

  • Hash crash or hash collision
  • efficiency
  • uniformity
  • collision resistance
  • pre-image resistance
  • second pre-image resistance

Hash algorithms can be used as..

  • encryption
  • store values
  • verify same files or not