Algorithms Flashcards

1
Q

space complexity for the sorting algorithms

A

IDK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

for(int i =0 ; I < n; I++)
for(j =0 ; j < I ; j++)
how man times does the second loop perform?
what is the complexity of these two?

A
  1. n * (n - 1) / 2
  2. O(n**2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if you have a difficult loops to analyze, what should you do?

A

you should trace the variables (I,j)..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is the complexity time for
for( I = 0 ; I < n, I++)

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the complexity time for
for( I = 0 ; I < n, I= I+2)

A

n/2 = O(N)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the complexity time for
for( I = 0 ; I < n, I–)

A

o(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the complexity time for
for( I = 0 ; I < n, I= I*2)

A

O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how we determined what sorting algorithm to use?

A
  1. Time Complexity
  2. Space Complexity
  3. Internal Sort or External Sort:
    Internal: load the data into memory
    External Sort: data on disk
  4. Recursive or Non recursive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

explain Selection sort shortly

A

select the minimum and and move it to the sorted part at the beginning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the psudu code for selection sort

A

for I in range(n -1):
iMin = I
for j in range(I+1, n):
if A[j] < A[iMin]:
iMin=j
temp = A[I]
A[I] = A[iMin]
A[iMin] = temp

com = O(N**2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

can you explain Bubble Sort shortly?

A

we move the large elements to the right by swapping the left element (large) with right element(small).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the pseudo code for Bubble sort?
how can you improve the regular Bubble sort?

A

for k in range(1, n - 1):
for I in range (0, n -k -1): # -k because the right elements are sorted.
If A[I] > A[I + 1]:
swap(A[I], A[I+1])

Com = O(N**2)
we can improve it by adding flag on the big loop, when we have swap we change it to true, and before we finish the big loop, we check if it’s false. so we can return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly