Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The partition function takes the last element in the array as the pivot, places the pivot element at correct position in sorted array, and places elements smaller than pivot to the left of pivot and elements greater than pivot to the right of pivot. And it returns the partitioning index used as new pivots in recursive quicksort.
The quicksort sorts the left and right sub-arrays about the pivot.

Output:

 

Extensions

 

Extension 2:

Demonstrate polymorphism in Go.

...

Explore the precedence and semantics of memory operators & and * in Go.


In Goyou cannot increment a pointer using ptr++.

Because increment and decrement operations can’t be used as expressions, only as statements. Also, only the postfix notation is allowed.

Without pointer arithmetic, the convenience value of By removing the pre- and postfix increment operators drops. By removing them from  from the expression hierarchy altogether, expression syntax is simplified and the messy issues around the order of evaluation of ++ and -- (consider f(i++) and p[i] = q[++i]) are eliminated as well. The simplification is significant.

Unary operators have the highest precedence. As the  ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.

In Go, you cannot increment a pointer using ptr++the first example, the pointer points to the address of n at first. Then it gets dereferenced--the value of the pointer is 20. After the increment, the value becomes 21. 

Image Added

In the second example, the pointer first points to index 0 of the array. The only way to increment the location of pointer is to specify the new index.

After moving the pointer to index 1 of the array, the dereferenced pointer gets evaluated to 2, which is the second element in the array.

Image Added


Resources

https://tour.golang.org/flowcontrol/12

...