Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

Defining a variable inside for loop

$
0
0

I want to make some basics clear :)

Now In insertion sort we use 2 for loops and inside the for loop we keep track of key element which is to be inserted

CODE

    for(int i = 0 ; i < n ; i++) 
`                  `int key = a[i]
                    int j = i-1
                    while j >= 0 && a[i] < a[j]:
                               a[j+1] = a[j]
                               j--
                    a[j+1] = a[i]

What difference it will make when we write code like this(predefining variables)

 int i,key,j;
    for(i = 0 ; i < n ; i++) 
    `                  `key = a[i]
                        j = i-1
                        while j >= 0 && a[i] < a[j]:
                                   a[j+1] = a[j]
                                   j--
                        a[j+1] = a[i]

Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>