Structure And Algorithm
Search, sort on memory main call it search and sort internal
1 Search Algorithm
1.1 LinearSearch: compare every element of array with x
* Cost
Best Case: O(1)
Middel Case: O(n+1/2) // use technique guards
Worst Case: O(n)
* Explain version basic:
Step 1: i = 0 start index head of array
Step 2: compare value of index i in array with x
array[i] == x return i // index appearance x in array
array[i] != x continue Step 3
Step 3: i = i++
if i > N return -1 // x not appearance in array
i < N repeat Step 2
* Code with technical guards
1.2 Search Binary:
Intended for array is order (asc,desc), the ideas check with element middle of array
if x < value middle it search the element before middle
if x > value middel it search the element after middle
else x == value of middle return middle
* Cost
Best Case: O(1)
Middel Case: O[log2n/2]
Worst Case: O(log2n)
* Explain :
Step 1: left = 0 , right = length array - 1
Step 2:
middle = (left + right) / 2
if x == middle return middle
if x > middle left = middle + 1 // search the elment from left ... right
if x < middle right = middle - 1 // search the elment from left ... right
Step 3: if left <= right repeat bước 2
else return -1
* Code
1.3 Sumary search :
Algorithm Binary searh quickly than LinearSeach
Cost: log2N < N
Example: N size of array is 100: log2(100) = 6 < 100
Require: Order <-> Not thing
GitHub:
2 Sort Algorithm
2.1 Concept sort
Ascending tăng (bring value max the tail):
Descending giảm (bring value max the head) :
2.2 Selection Sort (chọn trực tiếp):
Find min of current swap with a[i] current.
* Cost
Best Case: n(n-1) / 2
Worst Case: n(n-1) / 2
* Explain :
Step 1: i = 0 , min = i
Step 2: Find value min of array from j = i + 1 to end Array
Step 3: Swap value of min and value of i
Step 3: i++ if i < n , repeat Step 2
else Stop sort
* Code
2.3 Insertaion Sort (chèn trực tiếp):
Suppose array has a[0] orderd, then we add the elements from a[1] --> a[n-1] into current array, it call insertion sort
* Cost
Best Case: n-1 // minus 1 because it start with 1
Worst Case: n(n-1)/2 - 1
* Explain :
Step 1: i = 1
Step 2: foreach from index i-1 to i= 0 of head array if value of index > x, we move to it right(vòng lặp từ vị trí của i-1 đến đầu i=0 nếu giá trị lớn hơn x thì sẽ dời 1 đơn vị sáng phải)
Step 3: if value of index < x thì sẽ insert vào vị trí đó + 1
* Code
2.4 InterchangeSort (đổi chỗ trực tiếp):
* Cost
Best Case = Worst Case = n(n-1)/2
* Explain :
Step 1: i = 0
Step 2: j = i + 1 , tìm phần tử i < j mà a[i] > a[j] swap 2 giá trị
Step 3: j = j + 1 , và j < n lặp lại bước Step 2
Step 4: i = i + 1 , và i < n-1 lặp lại bước Step 2 + 3
* Code
2.5 BubbleSort (nổi bọt):
* Cost
Best Case = Worst Case = n(n-1)/2
* Explain :
Step 1: i = 0
Step 2: j = n-1 from index tail to head,
trong khi (i < j)
if a[j] < a[j-1] swap value, bring value smaller to head
j = j - 1
Step 3: i = i + 1
i < n repeat Step 2, ngược lại stop
* Code
Shaker Sort
This is advance of bubble sort
sort by 2 browse Go and return
Go: bring value smaler to head array
Return : bring value bigger to tail array
2.6 Quickly Sort
* Cost :
Best Case: n*log2(n)
Middle Case: n*log2(b)
Worst Case: n^2
Explain:
Step 1: x=a[k], i = left, j = right
Step 2: find the couple incorect
While (a[i] < x) i++
While (a[j] > x) j--
if i <= j , a[i] > a[j]
swap value, next i++, j--
Step 3: i < j , repeat Step 2
i >= J Stop
Code :
Git search and sort: https://github.com/nguyenthinhit996/sharefullcode/tree/master/structureWithAlgorithm
0 comments :
About Me

- Peter
- Tân An, Long An, Vietnam
- Hello everyone, I love programming, I love making friends with people all over the world.
Contact
Popular Posts
-
The one of Design Pattern Series, this is factory design for createnal pattern. Please read in the linked document. Link document: https:...
-
youtube tutorial: https://www.youtube.com/watch?v=IEf1KAcK6A8 1 let and scope, 2 const, 3 array function 4 default value 5 Object iteral ext...
-
1 Copy Object in java Use = for immutable class ex: Integer , String or Wrapper classes Object Use Contructor has paramater copy, ObjectA...
-
JPA find() & getReference() in javax.persistence.EntityManager * find (): always call select from DB Use for select one entity from DB...
-
Preface Trong java có 3 kiểu so sánh đặc trưng như sau: + Sử dụng toán tử == : return Boolean Primitive thì so sánh giá trị thực, Reference ...
-
Persistence Context has two implemnet is JPA EntityManage and Hiberbate Session Example use Session of hiberate has exist github you can vi...
-
1 check Null and Undefined And primative datatype if(object){ // object is not null, undefined, 0, '', false. but it can empty({})...
-
1) CRUD có thể sử dung find Compositekey hoặc findBy compositeket + column in composite 2) CRUD findBy column And column 3 Not LazyLoa...
-
This post use language vietnames is, the future is will introduce english version. The clean code for java also application for the others. ...
-
Parse LocalDateTime to String to pattern year : yyyy || 2021 month of year: M / MMM / MMMM || 06 / Jun / June da...
Post a Comment