| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
Share
About Exam
medium Solved by 3 students
Insertion Sort Algorithm Implementation
< Prev
Next >

Which of the following is an example of an insertion sort algorithm implementation?

int[] arr = {12, 11, 13, 5, 6};
for (int i = 1; i < arr.length; i++) {
    int key = arr[i];
    int j = i - 1;
    while(j >= 0 && arr[j] > key) {
        arr[j+1] = arr[j];
        j--;
    }
    arr[j+1] = key;
}
A

for (int i = 0; i < arr.length; i++) { for (int j = i+1; j < arr.length; j++) { if(arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }

B

int[] arr = {12, 11, 13, 5, 6}; for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while(j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j–; } arr[j+1] = key; }

C

Arrays.sort(arr);

D

int[] arr = {64, 25, 12, 22, 11}; for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if(arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; }

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1anvitakeni0111 0m 00s 100
#2ponneban00002 0m 00s -20
#3bommasam00025 12m 27s -577
Items per page:
10
1 – 3 of 3

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy