Linear search and bubble sort
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Find one mark, then order the list
- Syllabus topic 10.2 asks for two algorithms on an array: a linear search and a bubble sort.
- A linear search looks at every cell. Remember the index when the value matches. If it never matches, the index stays 0.
- Searching does not move the values. Sorting does.
한국어
한 점 찾기, 목록 정렬하기
- syllabus 주제 10.2는 배열에 대한 두 가지 알고리즘인 선형 검색과 버블 정렬을 요구합니다.
- 선형 검색은 모든 셀을 봅니다. 값이 일치할 때 인덱스를 기억하십시오. 절대 일치하지 않으면 인덱스는 0으로 남습니다.
- 검색은 값을 이동시키지 않습니다. 정렬은 이동시킵니다.
DECLARE A : ARRAY[1:4] OF INTEGER
DECLARE I, Pos : INTEGER
A[1] ← 4
A[2] ← 9
A[3] ← 1
A[4] ← 7
Pos ← 0
FOR I ← 1 TO 4
IF A[I] = 9 THEN
Pos ← I
ENDIF
NEXT I
OUTPUT Pos
English
Bubble the largest to the end
- Compare each pair of neighbours. When the left one is larger, swap them through a temporary variable.
- One comparison is not the whole sort. Repeat the pass until every pair has been handled.
- For four values, an outer loop of three passes and an inner loop across the pairs is enough.
한국어
가장 큰 값을 끝으로 버블링하기
- 각 쌍의 이웃을 비교합니다. 왼쪽이 크면 임시 변수를 통해交换하십시오.
- 하나의 비교만으로 전체 정렬이 아닙니다. 모든 쌍이 처리될 때까지 패스를 반복하십시오.
- 네 개의 값에 대해 외부 루프 3회와 내부 루프 쌍 전체는 충분합니다.
DECLARE Score : ARRAY[1:4] OF INTEGER
DECLARE I, J, Temp : INTEGER
Score[1] ← 4
Score[2] ← 1
Score[3] ← 3
Score[4] ← 2
FOR I ← 1 TO 3
FOR J ← 1 TO 3
IF Score[J] > Score[J + 1] THEN
Temp ← Score[J]
Score[J] ← Score[J + 1]
Score[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
OUTPUT Score[1]
OUTPUT Score[2]
OUTPUT Score[3]
OUTPUT Score[4]
English
Common mistakes
- A swap needs three assignments. Copying the right cell over the left one loses the left value.
THENstays on theIFline in this guide. The innerNEXTnamesJ, the outer namesI.
한국어
흔한 실수
- 스왑에는 세 번의 할당이 필요합니다. 오른쪽 셀을 왼쪽으로 복사하면 왼쪽 값이 손실됩니다.
THEN는 이 가이드에서IF줄에 머무릅니다. 내부NEXT은J을, 외부 이름은I을 사용합니다.
English
Now you try
- First report where
9sits. Then sort four values into ascending order, one per line.
한국어
이제 직접 해보기
- 먼저
9가 있는 위치를 보고, 네 값을 오름차순으로 정렬하여 줄마다 하나씩 출력하십시오.
The array A holds 4, 9, 1, 7. Set Pos to the index of 9 (it is 2) and output Pos. If you never find it, Pos stays 0.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The array Score holds 4, 1, 3, 2. Bubble-sort it into ascending order and output the four values, one per line.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.