Question

Im really trying to understand fully and PROPERLY. very lost on the topic. Please help me...

Im really trying to understand fully and PROPERLY. very lost on the topic. Please help me and give explaination of each steps. thank you so much
media%2Fc88%2Fc8884261-a4f6-4a2a-bf31-6f
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us assume size of array is N where each element is atmost k away from its target position. Now an algorithm that sorts inO (nlogk) time.

Examples:

1.Input-

a[] ={7,4,2,8,9,3,1}

k=3

Output-

a[]={1,2,3,4,7,8,9}

2 Input

a[] ={8,7,6,4,3,2,9,1}

k=8

Output

a[] ={1,2,3,4,6,7,8,9}

................Program to implement insertion sort........

static void insertionSort(int Arr[], int size)

{

int i, key, j;

for (i = 1; i < size; i++)

{

    key= Arr[i];

    j = i-1;

    while (j >= 0 && Arr[j] > key)

    {

‌   Arr[j+1] = Arr[j];

        j = j-1;

    }
Arr[j+1]=key;
}
}

.............

Inner loop will run at almost k times. To move each element to correct location at most k elements are required to move. So complexity will be O(nk).

You can also solve it with the help of Heap data structures.

Add a comment
Know the answer?
Add Answer to:
Im really trying to understand fully and PROPERLY. very lost on the topic. Please help me...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT