[์ด์ ๊ธ] 2024.07.28 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript : Sorting Algorithms (Advanced)
Data Structures & Algorithms with Javascript : Sorting Algorithms (Advanced)
2024.07.27 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript : Sorting Algorithms (basic) Data Structures & Algorithms with Javascript : Sorting Algorithms (basic)2024.07.26 - [๐ชฒ debug] - Data Structures & Algorithms
pyotato-dev.tistory.com
๋ฐ์ดํฐ๋ฅผ ํ์ ๋ฐฉ์์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋๋ค : sequential search์ binary search.
sequential search์ ๋ฆฌ์คํธ๊ฐ ๋๋ค ํ ์์๋ก ๋์ด ์์ ๊ฒฝ์ฐ ์ฌ์ฉ๋๊ณ , binary search๋ ์ ๋ ฌ๋ ๋ฆฌ์คํธ์์ ์์๋ฅผ ์ฐพ์ ๋ ์ฌ์ฉ๋๋ค.
binary search๊ฐ ์ข ๋ ํจ์จ์ ์ด์ง๋ง ํ์ ์ด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํด์ค์ผ ํ๋ค๋ ๊ฑธ ๊ฐ์ํด์ผ ํ๋ค.
์ค๋์ sequential search์ binary search๋ฅผ ๊ตฌํํด ๋ณด๊ณ ๊ฐ๊ฐ ์์๋ฅผ ๋ค๋ฉฐ ์ฌ์ฉ๋ ์ดํด๋ณด์.
Sequential Search
๊ฐ์ฅ ์ง๊ด์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋ ๋ฐฉ์์ ๊ทธ๋ฅ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๋ถํฐ ์์ํด์ ์ฐพ์ผ๋ ค๋ ์์์ ๋๋ฌํ ๋๊น์ง ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ ๊ฒ์ด๋ค. ์ด๋ฐ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋ ๋ฐฉ์์ sequential ๋๋ linear search๋ผ๊ณ ํ๋ค. brute-force ๋ฐฉ์์ด sequential search๋ฅผ ํ์ฉํ๋ค.
sequential search ๊ตฌํ์ ์์ฃผ ๋จ์ํ๋ค. ๋ฃจํ๋ฅผ ๋๋ฉด์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ดํด ์ฐพ์ผ๋ ค๋ ๊ฐ์ ์ฐพ์ ๋๊น์ง ๋ฐ๋ณตํ๋ฉด ๋๋ค.
function seqSearch(arr,data){
for(let i = 0; i < arr.length; ++i){
if(arr[i] === data){
return true; // ๊ฒ์ ์ ๊ฐ์ ์ฐพ์๋ค๋ฉด ์ฆ์ true ๋ฆฌํด
}
return false;
}
}
// array๋ฅผ ํ ์ค์ 10๊ฐ์ฉ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถํ ํ์์ผ๋ก ๋ฆฌํดํด์ฃผ๋ ํฌํผํจ์
function displayArr(arr){
return arr.reduce((acc,curr,i) => {
acc+=curr+' ';
if(i%10 ==9){
acc+="\n";
}
return acc;
},'')
}
// ์ถ๋ ฅ ํจ์
const print = item => console.log(item);
let nums = Array.from({length:100},()=>Math.floor(Math.random()*101));
const num = 32;
if(seqSearch(nums,num)){
print(`${num} is in the array`);
}else {
print(`${num} is not in the array`);
}
print(displayArr(nums));
/**
32 is not in the array
80 80 7 5 95 40 48 14 81 49
100 74 68 51 39 73 36 23 28 19
55 96 52 30 85 1 11 25 71 33
61 99 1 86 15 44 20 18 98 97
8 99 27 97 94 81 57 80 100 77
73 45 62 40 56 31 91 68 40 23
27 5 31 81 74 91 88 13 55 42
64 20 55 74 15 18 59 31 28 47
74 62 9 73 44 23 49 6 18 8
72 53 0 71 58 55 18 0 11 10
*/
์๋ฐ์คํฌ๋ฆฝํธ์๋ indexOf๋ผ๋ ๋ด์ฅ ํจ์๊ฐ ์๋ค.
indexOf๋ true/false ๋์ ์ฐพ์ผ๋ ค๋ ๊ฐ์ ์ธ๋ฑ์ค ํน์ ์๋ค๋ฉด -1์ ๋ฆฌํดํด์ค๋ค. seqSearch๊ฐ indexOf์ฒ๋ผ ๋์ํ๋๋ก ์์ ํด ๋ณด์.
function seqSearch(arr,data){
for(let i = 0; i < arr.length; ++i){
if(arr[i] === data){
return i; // ๊ฒ์ ์ ํด๋น ์์๊ฐ ์๋ฆฌํ ๋ฐฐ์ด์ ์ธ๋ฑ์ค ๋ฆฌํด
}
}
return -1;
}
์ต๋ ์ต์๊ฐ ์ฐพ๊ธฐ
์ ๋ ฌ๋ ๋ฐ์ดํฐ๊ฐ์์๋ ์ต๋์ต์ ๊ฐ์ ์ฐพ๋ ๊ฒ ์์ฃผ ์ฝ๋ค. ํ์ง๋ง ์ ๋ ฌ๋์ง ์์ ๋ฐ์ดํฐ์์ ์ฐพ๊ธฐ์๋ ์กฐ๊ธ ๋ ์ ๊ฒฝ ์ธ ๋ถ๋ถ๋ค์ด ์๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ต์๊ฐ์ ๊ตฌํ๊ธฐ ์ํ ๋ก์ง์ ์๊ฐํด ๋ณผ ์ ์๋ค.
- ๋ฐฐ์ด์ ์ฒซ ์์๋ฅผ ์ต์๊ฐ์ผ๋ก ์ค์ ํ๋ค.
- ๋ฐฐ์ด์ ๋ ๋ฒ์งธ ์์๋ถํฐ ๋ฃจํ๋ฅผ ๋๋ฉด์ ํ์ฌ ์ต์๊ฐ๊ณผ ๋์ ๋น๊ต๋ฅผ ํ๋ค.
- ํ์ฌ ์์๊ฐ ํ์ฌ ์ต์๊ฐ๋ณด๋ค ์๋ค๋ฉด ํ์ฌ ๊ฐ์ ์๋ก์ด ์ต์๊ฐ์ผ๋ก ์ง์ ํ๋ค.
- ๋ค์ ์ธ๋ฑ์ค์ ์๋ฆฌ๋จผํธ๋ก ์ฎ๊ฒจ์ 3๋ฒ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
- ์ต์๊ฐ์ ํ๋ก๊ทธ๋จ ์ข ๋ฃ ์์ ์๋ ๋ณ์๋ก ์ ์ฅ๋์ด ์์ด์ผ ํ๋ค.
์์ ๊ณผ์ ์ ์ฝ๋๋ก ์ฎ๊ฒจ๋ณด์.
function findMin(arr) {
let min = arr[0];
for (let i = 1; i < arr.length; ++i) { // ์ฒซ์งธ(ํ์ฌ ์ต์์ ์๋ฆฌ)๊ฐ ์๋ ๋๋ฒ์งธ ์์๋ถํฐ ๋น๊ต ์์
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
์ต๋๊ฐ์ ๊ตฌํ๋ ๊ณผ์ ๋ ๋น์ทํ๋ค.
๋ฐฐ์ด์ ์ฒซ ์์๋ฅผ ์ต์๋ก ์ง์ ํ๋ ๋์ ์ต๋๋ก ์ง์ ํด ์ฃผ๊ณ ๊ฐ ์์๋ค์ ์ํํ๋ฉด์ ๋์๋น๊ต๋ฅผ ํด์ ์ต๋๋ฅผ ์ง์ ํด ์ฃผ๋ฉด ๋๋ค.
function findMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; ++i) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
๐ ์ ์ฒด ์ฝ๋
function seqSearch(arr,data){
for(let i = 0; i < arr.length; ++i){
if(arr[i] === data){
return true; // ๊ฒ์ ์ ๊ฐ์ ์ฐพ์๋ค๋ฉด ์ฆ์ true ๋ฆฌํด
}
return false;
}
}
// array๋ฅผ ํ ์ค์ 10๊ฐ์ฉ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถํ ํ์์ผ๋ก ๋ฆฌํดํด์ฃผ๋ ํฌํผํจ์
function displayArr(arr){
return arr.reduce((acc,curr,i) => {
acc+=curr+' ';
if(i%10 ==9){
acc+="\n";
}
return acc;
},'')
}
// ์ถ๋ ฅ ํจ์
const print = item => console.log(item);
let nums = Array.from({length:100},()=>Math.floor(Math.random()*101));
// ์ต์๊ฐ ๊ตฌํ๊ธฐ
function findMin(arr) {
let min = arr[0];
for (let i = 1; i < arr.length; ++i) { // ์ฒซ์งธ(ํ์ฌ ์ต์์ ์๋ฆฌ)๊ฐ ์๋ ๋๋ฒ์งธ ์์๋ถํฐ ๋น๊ต ์์
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
// ์ต๋๊ฐ ๊ตฌํ๊ธฐ
function findMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; ++i) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
const minValue = findMin(nums);
const maxValue = findMax(nums);
print(displayArr(nums));
print();
print("The minimum value is: " + minValue);
print();
print("The maximum value is: " + maxValue);
/**
51 48 76 65 23 96 22 56 74 37
38 3 6 62 44 25 73 22 76 43
66 91 21 81 61 60 94 32 92 57
13 8 18 57 37 12 1 78 0 55
37 26 99 62 38 33 75 9 92 84
75 76 48 70 100 77 94 1 65 63
11 82 5 64 48 36 29 90 20 2
2 52 41 39 59 10 4 18 87 13
52 3 3 98 63 50 91 68 44 49
0 36 40 50 41 77 56 14 96 90
The minimum value is: 0
The maximum value is: 100
*/
Self-Organizing data
sequential search๊ฐ ๊ฐ์ฅ ๋น ๋ฅด๊ฒ ์ด๋ฃจ์ด์ง๊ธฐ ์ํด์๋ ๊ฐ๋จํ ์ฐพ์ผ๋ ค๋ ์์๊ฐ ๋ฐฐ์ด ๋งจ ์์ ์์นํ๋๋ก ํ๋ ๊ฒ์ด๋ค.
์์ฃผ ๊ฒ์๋๋ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์๋ค๋ฉด ์ด ๊ฐ์ ๋ฐฐ์ด์ ๋งจ ์์ ์์นํ๋ ๋ฐฉ์์ผ๋ก ๊ฒ์ ์๊ฐ์ ์ค์ผ ์ ์๋ค.
์๋ฅผ ๋ค๋ฉด, ๋์๊ด์์ ์ธ๊ธฐ ์ฑ ์ด ์์ด ๊ทธ ์ฑ ์ ์์น๋ฅผ ๋ฌผ์ด๋ณด๋ ์ฌ๋์ด ๋ง๋ค๋ฉด ๊ฐ๊น์ด ๊ณณ์ ๊ทธ ์ฑ ์ ๋น์นํ๋ ๊ฒ์ด ์ข๋ค.
๊ฐ๋ฐ์๊ฐ ์ง์ ํ๋ก๊ทธ๋จ์ด ์คํ๋๊ธฐ ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๊ฒ์ด ์๋๋ผ ํ๋ก๊ทธ๋จ ์์ฒด๊ฐ ๋์๊ฐ๋ฉด์ ์ ๋ ฌ์ ํ๋ ๋ฐฉ์์ self-organized data๋ผ๊ณ ๋ถ๋ฅธ๋ค.
self-organized data๋ฅผ ๋ค๋ฃฐ ๋ ์์ฃผ ๊ฒ์๋๋ ๋ฐ์ดํฐ๋ฅผ "80-20 ๊ท์น"์ ๋ฐ๋ฅด๋๋ก ํ์.
์ฆ, 80% ์ ๊ฒ์์ด 20%์ ๋ฐ์ดํฐ๋ง ์ฐพ์ ๊ฒฝ์ฐ self-organize ํ๋๋ก ํ์.
self-organization์ ํตํด ๊ฒฐ๊ตญ ์์ฃผ ๊ฒ์๋๋ 20%๊ฐ ๋ฐ์ดํฐ ๋ฐฐ์ด ์์ ์์นํ๊ฒ ๋์ด ๊ฒ์์ด ๋ ๋นจ๋ผ์ง๋ค.
80-20 ๊ท์น์ฒ๋ผ ํ๋ฅ ๋ถํฌ ๊ท์น๋ค์ Pareto distributions๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์ด์ seqSearch ํจ์์ self-organization ๊ธฐ๋ฅ์ ์ถ๊ฐํด ๋ณด์.
4๊ฐ 3๋ฒ์ด๋ ๊ฒ์์ด ๋๊ธฐ ๋๋ฌธ์ ๋ฆฌ์คํธ์ ๋งจ ์์ผ๋ก bubble up ๋๊ณ ์๋ ๋ชจ์ต์ ํ์ธํ ์ ์๋ค.
function seqSearch(arr,data){
for(let i = 0; i < arr.length; ++i){
if(arr[i] === data){
if(i>0){
swap(arr,i,i-1);
}
return true;
}
}
return false;
}
function swap(arr, index, index1) {
temp = arr[index];
arr[index] = arr[index1];
arr[index1] = temp;
}
const print = item => console.log(item);
let numbers = [5,1,7,4,2,10,9,3,6,8];
print(numbers);
for (let i = 1; i <= 3; i++) {
seqSearch(numbers, 4);
print(numbers);
}
/**
[5, 1, 7, 4, 2, 10, 9, 3, 6, 8]
[5, 1, 4, 7, 2, 10, 9, 3, 6, 8]
[5, 4, 1, 7, 2, 10, 9, 3, 6, 8]
[4, 5, 1, 7, 2, 10, 9, 3, 6, 8]
*/
๊ฒ์ํ๋ ค๋ ์์๊ฐ ์ด๋ฏธ ๋ฐฐ์ด ์์ชฝ์ ์์นํ๋ค๋ฉด ์ด๋์ ์์ผ์ฃผ์ง ์๋๋ก ํ์.
80-20 ๊ท์น์ ๋ฐ๋ฅด๋๋ก ๋ฐฐ์ด ๊ธธ์ด์ 20%๋ฐ์ ์์นํ ๊ฒฝ์ฐ์๋ง ์ด๋์ ์ํค๋๋ก ์ฝ๋๋ฅผ ์์ ํด ๋ณด์.
function seqSearch(arr,data){
for(let i = 0; i < arr.length; ++i){
if(arr[i] === data && i > (arr.length * 0.2)){
swap(arr,i,0);
return true;
} else if(arr[i] == data){
return true;
}
}
return false;
}
function swap(arr, index, index1) {
temp = arr[index];
arr[index] = arr[index1];
arr[index1] = temp;
}
const print = item => console.log(item);
let numbers = [5,1,7,4,2,10,9,3,6,8];
print(numbers);
for (let i = 1; i <= 3; i++) {
seqSearch(numbers, 4);
print(numbers);
}
/**
[5, 1, 7, 4, 2, 10, 9, 3, 6, 8]
[4, 1, 7, 5, 2, 10, 9, 3, 6, 8]
[4, 1, 7, 5, 2, 10, 9, 3, 6, 8]
[4, 1, 7, 5, 2, 10, 9, 3, 6, 8]
*/
Binary Search
์ ๋ ฌ๋ ๋ฐ์ดํฐ์ ๊ฒฝ์ฐ sequential search๋ณด๋ค๋ binary search๋ฅผ ํ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด๋ค.
binary search๊ฐ ๋์ํ๋ ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ์ ๊ฒ์์ ํ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
์น๊ตฌ๋ ์ซ์ ๋ง์ถ๊ธฐ ๊ฒ์์ ํ๋ค๊ณ ํด๋ณด์. ๊ฐ๋ฅํ ์ซ์๋ 1๊ณผ 100 ์ฌ์ด์ ๊ฐ์ด๋ค.
์ซ์๋ฅผ ํ๋ ์ธ์น ๋๋ง๋ค ์น๊ตฌ๊ฐ ๋๋ตํ ์ ์๋ ๋ง์ ๋ค์ ์ค ํ๋๋ค.
- ์ ๋ต์ ๋๋ค!
- ์ซ์๊ฐ ๋๋ฌด ํฌ๋ค!
- ์ซ์๊ฐ ๋๋ฌด ์๋ค!
์ด ๊ฒ์์์๋ ํญ์ ์ค๊ฐ ์์น์ ์ซ์๋ฅผ ์ ํํ๋ ๊ฒ ์ ์ ์ ํ์ ํญ์ ์ค์ด๊ธฐ ๋๋ฌธ์ ์ ๋ฆฌํ๋ค.
100์ ๋ฐ์ธ 50์ ์ธ์ณค์ ๊ฒฝ์ฐ ์ ๋ต์ด ๋ ํฐ ์๋ผ๋ฉด 75๋ฅผ ์ธ์ณ๋ณด๊ณ , ์๋ค๋ฉด 25๋ฅผ ์ธ์น๋ค.
๋ค์์ ์ ๋ต์ด 82์ธ ๊ฒฝ์ฐ๋ฅผ ๋ํ๋ธ๋ค.
๋ง์ฝ 1๋ถํฐ ์ฐจ๋ก๋๋ก ์ธ์ณค๋ค๋ฉด 82๋ฒ์งธ๊น์ง ํด์ด ์ง์๋๋ค. 100์ ์ธ์น๋๋ผ๋ 18๋ฒ์งธ๊น์ง ํด์ด ์ง์๋๋ค.
ํ์ง๋ง ์ค๊ฐ ์ง์ ๋ถํฐ ์ธ์น๋ฉด 6๋ฒ ์์ ์ ๋ต์ ๋งํ ์ ์๋ค.
์ด์ binary search๊ฐ ๋ฌด์์ธ์ง ์ดํด๋ดค๋ค. binary search์ ๋ ผ๋ฆฌ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด์.
- ๋ฐฐ์ด์ ์ฒซ ์์์ ํ์ ๋ฒ์ (lower bound, 0)๋ฅผ ์ง์ .
- ๋ฐฐ์ด์ ๋ง์ง๋ง ์์์ ์์ ๋ฒ์ (upper bound, array.length-1)๋ฅผ ์ง์ .
- ํ์ ๋ฒ์๊ฐ ์์ ๋ฒ์๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด
- ์ค๊ฐ์ (midpoint) = (์์ ๋ฒ์ - ํ์ ๋ฒ์)/2๋ก ์ง์ .
- ์ค๊ฐ์ ์ ์์๊ฐ ์ฐพ์ผ๋ ค๋ ์์๋ณด๋ค ์๋ค๋ฉด ์๋ก์ด ํ์ ๋ฒ์๋ฅผ ์ค๊ฐ์ + 1๋ก ์ง์
- ์ค๊ฐ์ ์ ์์๊ฐ ์ฐพ์ผ๋ ค๋ ์์๋ณด๋ค ํฌ๋ฉด ์๋ก์ด ์์ ๋ฒ์๋ฅผ ์ค๊ฐ์ - 1๋ก ์ง์
- ์ค๊ฐ์ ์ ์์ == ์ฐพ์ผ๋ ค๋ ์์๋ผ๋ฉด ์ค๊ฐ์ ๋ฐํ
// ์ถ๋ ฅ ํจ์
const print = item => console.log(item);
function binarySearch(arr,data){
let upperBound = arr.length-1;
let lowerBound = 0;
while(lowerBound <= upperBound){
let mid = Math.floor((upperBound+lowerBound)/2);
print(`current midpoint: ${mid}`);
if(arr[mid]<data){
lowerBound = mid+1;
} else if(arr[mid]>data){
upperBound = mid-1;
} else {
return mid;
}
}
return -1;
}
// array๋ฅผ ํ ์ค์ 10๊ฐ์ฉ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถํ ํ์์ผ๋ก ๋ฆฌํดํด์ฃผ๋ ํฌํผํจ์
function displayArr(arr){
return arr.reduce((acc,curr,i) => {
acc+=curr+' ';
if(i%10 ==9){
acc+="\n";
}
return acc;
},'')
}
// insertSort, ๋ฐ์ดํฐ ์ ๋ ฌํด์ฃผ๊ธฐ
function insertSort(arr){
const copy = [...arr];
let temp, inner;
for(let outer = 1; outer <= copy.length-1; ++outer){
temp = copy[outer];
inner = outer;
while(inner > 0 && (copy[inner-1] >= temp)){
copy[inner] = copy[inner-1];
--inner;
}
copy[inner] = temp;
}
return copy;
}
let nums = Array.from({length:100},()=>Math.floor(Math.random()*101));
nums = insertSort(nums); // ์ ๋ ฌ ์ดํ
print(displayArr(nums));
print();
const val = 99;
let retVal = binarySearch(nums, val);
if (retVal >= 0) {
print("Found " + val + " at position " + retVal);
} else {
print(val + " is not in array.");
}
/**
1 1 2 3 6 7 7 8 9 10
10 13 13 14 14 14 16 16 18 18
18 19 19 21 22 23 25 25 25 25
26 27 28 29 30 31 33 34 35 35
35 36 36 37 37 38 39 41 42 42
42 43 45 46 48 49 50 52 52 54
54 56 58 59 61 61 63 63 63 66
66 66 67 70 71 71 72 73 74 75
75 76 78 78 81 84 84 85 85 86
87 87 87 90 90 92 93 96 96 99
current midpoint: 49
current midpoint: 74
current midpoint: 87
current midpoint: 93
current midpoint: 96
current midpoint: 98
current midpoint: 99
Found 99 at position 99
*/
๋๋ค ๊ฐ์ผ๋ก ๋ฐฐ์ด์ ์ฑ์ฐ๋ค ๋ณด๋ฉด ๊ฐ์ ๊ฐ๋ค์ด ์ค๋ณต๋์ด ๋ค์ด๊ฐ ์ ์๋ค.
๊ทผ๋ฐ ์์ ๋ฐฐ์ด์ ์ฐพ์ผ๋ ค๋ ๊ฐ์ ์ค๋ณต๊ฐ๋ค ์ค ๊ฐ์ด๋ฐ ๊ฐ์ ์ฐพ๋๋ค.
์๋ฅผ ๋ค์ด 66์ด 3๋ฒ ๋ค์ด๊ฐ ์์ ๋ฐ์ดํฐ์์ ๋ ๋ฒ์งธ 66์ ์๋ฆฌ๋ฅผ ๋ฐํํ๋ค.
๊ฐ์ด ๋ช ๋ฒ ๋ฑ์ฅํ๋์ง ๋ํ๋ผ ์ ์๋๋ก ์ฝ๋๋ฅผ ์์ ํด ๋ณด์.
// ์ถ๋ ฅ ํจ์
const print = item => console.log(item);
function binarySearch(arr,data){
let upperBound = arr.length-1;
let lowerBound = 0;
while(lowerBound <= upperBound){
let mid = Math.floor((upperBound+lowerBound)/2);
print(`current midpoint: ${mid}`);
if(arr[mid]<data){
lowerBound = mid+1;
} else if(arr[mid]>data){
upperBound = mid-1;
} else {
return mid;
}
}
return -1;
}
///////////////////////////////// ๐ ์ถ๊ฐ /////////////////////////////////
// ๋ฑ์ฅ ๋น๋์ ์นด์ดํธ
function count(arr, data){
let count = 0;
let position = binarySearch(arr,data);
if(position > -1){
++count;
for(let i=position-1; i>0; --i){
if(arr[i]==data){
++count;
} else {
break;
}
}
for(let i=position+1; i<arr.length; ++i){
if(arr[i]==data){
++count;
} else {
break;
}
}
}
return count;
}
///////////////////////////////// ๐ ์ถ๊ฐ /////////////////////////////////
// array๋ฅผ ํ ์ค์ 10๊ฐ์ฉ ๋์ด์ฐ๊ธฐ๋ก ๊ตฌ๋ถํ ํ์์ผ๋ก ๋ฆฌํดํด์ฃผ๋ ํฌํผํจ์
function displayArr(arr){
return arr.reduce((acc,curr,i) => {
acc+=curr+' ';
if(i%10 ==9){
acc+="\n";
}
return acc;
},'')
}
// insertSort, ๋ฐ์ดํฐ ์ ๋ ฌํด์ฃผ๊ธฐ
function insertSort(arr){
const copy = [...arr];
let temp, inner;
for(let outer = 1; outer <= copy.length-1; ++outer){
temp = copy[outer];
inner = outer;
while(inner > 0 && (copy[inner-1] >= temp)){
copy[inner] = copy[inner-1];
--inner;
}
copy[inner] = temp;
}
return copy;
}
let nums = Array.from({length:100},()=>Math.floor(Math.random()*101));
nums = insertSort(nums); // ์ ๋ ฌ ์ดํ
print(displayArr(nums));
print();
const val = 45;
let retVal = count(nums, val);
if (retVal >= 0) {
print("Found " + retVal + " occurrences of " + val + ".");
} else {
print(val + " is not in array.");
}
/**
1 2 5 7 7 8 9 10 10 12
13 15 15 15 15 15 16 18 18 18
19 19 23 24 24 24 25 25 26 28
29 31 31 33 33 33 35 35 36 36
36 36 37 38 41 45 45 47 48 48
49 50 50 51 51 53 54 56 56 57
59 59 61 62 63 63 66 66 67 67
68 69 70 71 71 72 74 75 77 77
78 79 82 82 82 85 87 89 89 92
92 93 93 94 94 95 97 98 98 99
current midpoint: 49
current midpoint: 24
current midpoint: 36
current midpoint: 42
current midpoint: 45
Found 2 occurrences of 45.
*/
ํ ์คํธ ๋ฐ์ดํฐ ๊ฒ์
์ง๊ธ๊น์ง๋ ์ซ์ ๋ฐ์ดํฐ ๊ฒ์์ ๋ค๋ค๋ค. ์ด๋ฒ์๋ ๋ฌธ์์ด์ ๊ฒ์ํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ์ดํด๋ณด์.
[์ถ์ฒ] : https://www.norvig.com/big.txt
The nationalism of Hamilton was undemocratic. The democracy of Jefferson was, in the beginning, provincial. The historic mission of uniting nationalism and democracy was in the course of time given to new leaders from a region beyond the mountains, peopled by men and women from all sections and free from those state traditions which ran back to the early days of colonization. The voice of the democratic nationalism nourished in the West was heard when Clay of Kentucky advocated his American system of protection for industries; when Jackson of Tennessee condemned nullification in a ringing proclamation that has taken its place among the great American state papers; and when Lincoln of Illinois, in a fateful hour, called upon a bewildered people to meet the supreme test whether this was a nation destined to survive or to perish. And it will be remembered that Lincoln's party chose for its banner that earlier device--Republican--which Jefferson had made a sign of power. The "rail splitter" from Illinois united the nationalism of Hamilton with the democracy of Jefferson, and his appeal was clothed in the simple language of the people, not in the sonorous rhetoric which Webster learned in the schools.
์ด ํ ์คํธ ๋ฐ์ดํฐ์์ "rhetoric"์ด๋ผ๋ ๋จ์ด๋ฅผ ์ฐพ์ ๋ sequential search, binary search ๊ฐ๊ฐ์ผ๋ก ๋ฐ์ดํฐ ์ฐพ๊ธฐ์ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ฌ์ด ๋ณด์.
์ฒ๋ฆฌ๊ธฐ์ ์๋๊ฐ ๋งค์ฐ ๋นจ๋ผ์ ธ์ ๋ฐ์ดํฐ๋์ด ์์ฒญ ๋ง์ง ์์ ์ด์ ๋ ๊ฒ์ ์๊ณ ๋ฆฌ์ฆ์ ์ฐจ์ด๋ฅผ ๋๋ผ๊ธฐ ์ด๋ ต์ง๋ง,
์ํ์ ์ผ๋ก ์ฆ๋ช ๋ ๋ฐ๋ก๋ binary search๊ฐ ๊ฒ์ํ ์์ดํ ์ ๋ฐ์ฉ ์ค์ฌ๊ฐ๊ธฐ ๋๋ฌธ์ ๋ ๋น ๋ฅด๋ค.
// ์ถ๋ ฅ ํจ์
const print = item => console.log(item);
// ํ
์คํธ ๊ฒ์
function seqSearch(arr, data) {
for (let i = 0; i < arr.length; ++i) {
if (arr[i] == data) {
return i;
}
}
return -1;
}
function binarySearch(arr,data){
let upperBound = arr.length-1;
let lowerBound = 0;
while(lowerBound <= upperBound){
let mid = Math.floor((upperBound+lowerBound)/2);
if(arr[mid]<data){
lowerBound = mid+1;
} else if(arr[mid]>data){
upperBound = mid-1;
} else {
return mid;
}
}
return -1;
}
// insertSort, ๋ฐ์ดํฐ ์ ๋ ฌํด์ฃผ๊ธฐ
function insertSort(arr){
const copy = [...arr];
let temp, inner;
for(let outer = 1; outer <= copy.length-1; ++outer){
temp = copy[outer];
inner = outer;
while(inner > 0 && (copy[inner-1] >= temp)){
copy[inner] = copy[inner-1];
--inner;
}
copy[inner] = temp;
}
return copy;
}
const text = `The nationalism of Hamilton was undemocratic. The democracy of Jefferson was, in the beginning, provincial. The historic mission of uniting nationalism and democracy was in the course of time given to new leaders from a region beyond the mountains, peopled by men and women from all sections and free from those state traditions which ran back to the early days of colonization. The voice of the democratic nationalism nourished in the West was heard when Clay of Kentucky advocated his American system of protection for industries; when Jackson of Tennessee condemned nullification in a ringing proclamation that has taken its place among the great American state papers; and when Lincoln of Illinois, in a fateful hour, called upon a bewildered people to meet the supreme test whether this was a nation destined to survive or to perish. And it will be remembered that Lincoln's party chose for its banner that earlier device--Republican--which Jefferson had made a sign of power. The "rail splitter" from Illinois united the nationalism of Hamilton with the democracy of Jefferson, and his appeal was clothed in the simple language of the people, not in the sonorous rhetoric which Webster learned in the schools.`;
let words = text.split(" ");
const word = "rhetoric";
words = insertSort(words);
let start = new Date().getTime();
let position = seqSearch(words, word);
let stop = new Date().getTime();
let elapsed = stop - start;
if (position >= 0) {
print("Found " + word + " at position " + position + ".");
print("Sequential search took " + elapsed + " milliseconds.");
} else {
print(word + " is not in the file.");
}
/**
position = seqSearch(words, word)์ผ ๊ฒฝ์ฐ
Found rhetoric at position 190.
Sequential search took 1 milliseconds.
position = binarySearch(words, word)์ผ ๊ฒฝ์ฐ
Found rhetoric at position 136.
VM48:2 Sequential search took 0 milliseconds.
*/
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9