//C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input ar
ID: 3760665 • Letter: #
Question
//C code
int main(void)
{
int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array
int arraySize = sizeof(array)/sizeof(array[0]);
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
//Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm
j++; // j= sorted elements
int i=0;
/* "arraySize - j" is used because after the first run of the array , the last element of the
array will be sorted , so j=1 and now we have to sort arraySize-1 elements, then after
the second run of the array the next to last element will be sorted so j=2 and now we
have to sort arraySize-2 elements. */
for ( i = 0; i < arraySize - j; i++) {
if (array[i] > array[i + 1]) {
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
int i=0;
//print sorted array
for(i=0; i < arraySize; i++){
printf("%d", array[i]);
}
return 0;
}
Explanation / Answer
Below is the function for bubble sort.
.Ltext0:
.globl bubbleSort
bubbleSort:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 48897DE8 movq %rdi, -24(%rbp)
0008 8975E4 movl %esi, -28(%rbp)
000b 8B45E4 movl -28(%rbp), %eax
000e 83E801 subl $1, %eax
0011 8945F4 movl %eax, -12(%rbp)
0014 E9AE0000 jmp .L2
00
.L6:
0019 C745F801 movl $1, -8(%rbp)
000000
0020 E9920000 jmp .L3
00
.L5:
0025 8B45F8 movl -8(%rbp), %eax
0028 4898 cltq
002a 48C1E002 salq $2, %rax
002e 488D50FC leaq -4(%rax), %rdx
0032 488B45E8 movq -24(%rbp), %rax
0036 4801D0 addq %rdx, %rax
0039 8B10 movl (%rax), %edx
003b 8B45F8 movl -8(%rbp), %eax
003e 4898 cltq
0040 488D0C85 leaq 0(,%rax,4), %rcx
00000000
0048 488B45E8 movq -24(%rbp), %rax
004c 4801C8 addq %rcx, %rax
004f 8B00 movl (%rax), %eax
0051 39C2 cmpl %eax, %edx
0053 7E5E jle .L4
0055 8B45F8 movl -8(%rbp), %eax
0058 4898 cltq
005a 48C1E002 salq $2, %rax
005e 488D50FC leaq -4(%rax), %rdx
0062 488B45E8 movq -24(%rbp), %rax
0066 4801D0 addq %rdx, %rax
0069 8B00 movl (%rax), %eax
006b 8945FC movl %eax, -4(%rbp)
006e 8B45F8 movl -8(%rbp), %eax
0071 4898 cltq
0073 48C1E002 salq $2, %rax
0077 488D50FC leaq -4(%rax), %rdx
007b 488B45E8 movq -24(%rbp), %rax
007f 4801C2 addq %rax, %rdx
0082 8B45F8 movl -8(%rbp), %eax
0085 4898 cltq
0087 488D0C85 leaq 0(,%rax,4), %rcx
00000000
008f 488B45E8 movq -24(%rbp), %rax
0093 4801C8 addq %rcx, %rax
0096 8B00 movl (%rax), %eax
0098 8902 movl %eax, (%rdx)
009a 8B45F8 movl -8(%rbp), %eax
009d 4898 cltq
009f 488D1485 leaq 0(,%rax,4), %rdx
00000000
00a7 488B45E8 movq -24(%rbp), %rax
00ab 4801C2 addq %rax, %rdx
00ae 8B45FC movl -4(%rbp), %eax
00b1 8902 movl %eax, (%rdx)
.L4:
00b3 8345F801 addl $1, -8(%rbp)
.L3:
00c3 836DF401 subl $1, -12(%rbp)
00b7 8B45F8 movl -8(%rbp), %eax
00ba 3B45F4 cmpl -12(%rbp), %eax
00bd 0F8E62FF jle .L5
FFFF
.L2:
00c7 837DF400 cmpl $0, -12(%rbp)
00cb 0F8F48FF jg .L6
FFFF
00d1 5D popq %rbp
.cfi_def_cfa 7, 8
00d2 C3 ret
.cfi_endproc
.LFE0:
.Letext0:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.