Popular Posts

이은한. Powered by Blogger.

2022년 3월 16일 수요일

Delete all spaces in String at JAVA


By String method

yourString.replaceAll(" ", "");

By regular expression

yourString.replaceAll("\\p{Z}", "");

Explanation

Most situations, using the string method will be good enough to delete all spaces in String. 


However, computers have some charsets that represent spaces such as IDEOGRAPHIC SPACE. 


In this case, use the regular expression to delete all the spaces.


Example

        String yourString = " this is\u3000test ru n";
        System.out.println(yourString);

        String byMethod = yourString.replaceAll(" ", "");
        System.out.println("byMethod: "+byMethod);

        String byRegularExpression = yourString.replaceAll("\\p{Z}", "");
        System.out.println("byRegularExpression: "+byRegularExpression);

Result

 this is test ru n // original String
thisis testrun // by method
thisistestrun // by regular expression

2022년 3월 14일 월요일

install [JAVA-AdoptOpenJDK] to [windows 11]


purpose

install [JAVA-AdoptOpenJDK] to [windows 11]

Java?

One of programming language. For using, you need the programm calls Java Development Kit(JDK). The most famous JDK name is Java. It same as programming language name.

Short history

The Java JDK was open source. But, the Oracle bought the JDK and make it is not open source. Thus, there are many JDK programs.

AdoptOpenJDK?

One of the JDK programs.

environment variable?

Some writings may say that you need to set environment variable for window users. Nowaday, the JDK will set up automatically. You do not have to set up.

steps

1. download AdoptOpenJDK

official link

2. pick version

today (10-09-2021), there are version 16. However, I will download 11 for stability.
Higher version will have more functions but it may cause issues that you did not make.

3. setting

If you see this to install the JAVA, you will not need to set up anything. Just remember the place that where the JDK location.

Integrated development environment(IDE)?

IDE is program that help you code the programming languages. It helps to fix bugs, mistyping, and searching.

IDEs for JAVA

  1. intellij
  2. eclipse

There are many other IDEs. But, these two is most famous and popular

JDK setting?

If you use IDE first time, you will need to set JDK location as the blow.

*this picture is using intellij

2022년 3월 7일 월요일

How to find the Largest Difference in an Array


How to find the Largest Difference in an Array

Example

[7,1,5,3,6] -> 6 (7-1)

JAVA code

    public static int getMaxDifferNumFromArray(int[] input) {
        return getMaxFromArray(input) - getMinFromArray(input);
    }
    public static int getMaxFromArray(int[] input) {
        int max = Integer.MIN_VALUE;
        for (int temp : input) {
            if (max < temp) {
                max = temp;
            }
        }
        return max;
    }
    public static int getMinFromArray(int[] input) {
        int min = Integer.MAX_VALUE;
        for (int temp : input) {
            if (min > temp) {
                min = temp;
            }
        }
        return min;
    }

the smallest element has to be before the biggest element

Example

[7,1,5,3,6] -> 5 (1-6)

JAVA code

    public static int getMaxDifferNumSmallFirstFromArray(int[] input) {
        int min = Integer.MAX_VALUE;
        int rtn = 0;
        int temp = 0;

        for (int element : input) {
            if (min < element) {
                temp = element - min;
                if (temp > rtn)
                    rtn = temp;
            } else {
                min = element;
            }
        }
        return rtn;
    }

the biggest element has to be before the smallest element

Example

[1,7,5,3,6] -> 4 (7-3)

JAVA code

    public static int getMaxDifferNumBigFirstFromArray(int[] input) {
        int max = Integer.MIN_VALUE;
        int rtn = 0;
        int temp = 0;

        for (int element : input) {
            if (max > element) {
                element = max - element;
                if (element > rtn)
                    rtn = element;
            } else {
                max = element;
            }
        }
        return rtn;
    }

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)