Sort

2022. 1. 19. 18:06·data structure

sort: to arrange something in an order
to use fast algorithms ex. binary search, array should be sorted

1. bubble sort
- not used very much (there are a lot of better algorithms)
- select first 2 items of an array and compare them
> if left is bigger than the right, swap them
> do the same with index 1, 2
> repeat ….
> when it reaches the end of an array, the first cycle of bubble sort ends
> do it all over again from the first item
- time complexity = O(N^2)

2. selection sort
- keep track of the location of the lowest item from the first item in a variable
> swap the lowest item with the first item
> next cycle starts from the second item
- time complexity = O(N^2)
- faster than bubble sort (less swaps occur)

ex. [5, 2, 6, 3, 1, 4]
int a = lowest item
a = 5 > a = 2 > a = 2 > a = 2 > a= 1 > a = 4
swap a with 5
start from second item (2)

3. insertion sort
- faster than selection sort (selection sort scans all the elements, insertion sort scans items that are needed)
- time complexity = O(N^2)


three sort algorithms are expressed the same in the Big O notation, but their speed may differ.

'data structure' 카테고리의 다른 글

Type of Time Complexity (Big O notation)  (0) 2022.01.19
Search Algorithms (Binary Search/ Linear Search)  (0) 2022.01.19
Array  (0) 2022.01.19
'data structure' 카테고리의 다른 글
  • Type of Time Complexity (Big O notation)
  • Search Algorithms (Binary Search/ Linear Search)
  • Array
summercat
summercat
세젤귀 또킨이 집사 블로그
  • summercat
    여름고양이
    summercat
  • 전체
    오늘
    어제
    • 분류 전체보기 (28)
      • Swift-iOS (4)
        • iOS (1)
        • Swift (2)
      • 네트워크 (1)
      • data structure (4)
      • Git (6)
      • Java (13)
        • project lion JSB the origin (10)
        • ByteDegree (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    URLQueryItem
    HTTP message
    universal link
    Github
    야곰아카데미
    HTTP Methods
    deeplink
    OSI
    어플
    ssh-configure
    TCP
    앱개발
    Cow
    메모리 구조
    multipart/form-data
    URLComponents
    ssh-add
    부트캠프
    유니버셜링크
    SWIFT
    JSON
    copy on write
    딥링크
    IOS
    참조 타입
    커리어스타터캠프
    네트워크
    ssh-agent
    Endpoint
    값 타입
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
summercat
Sort
상단으로

티스토리툴바