Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###Iterators

Iterators

Currently you can sort anything that uses int*. But an iterator is a data type that has the same behavior as a pointer (when used in appropriate contexts). But there are other types that are iterator; so using them makes your code much more generalized.

template<typename I>
int QuickSort(I begin, I end)

Now your sort is generalized for any type that supports iterators (a pointer can be used as an iterator into a C-Array). But you can now also sort vector's lists and any other standard container type.

###Don't Declare variables before you use them

Don't Declare variables before you use them

int Temp = 0; //Variable used for swaping array members

This is declared way at the top.
You should declare variables just before you need them. This helps in the readability of the code (as I don't have to scroll back to the top of the function to find out what the code type is).

Also it prevents you wasting an instruction initializing a value that you will never use (here you init to zero but is that just a waste). If you declare at the point of usage you avoid that waste.

 int Temp = Array[Wall]; // declaration and initialization
 Array[Wall] = Array[Index];
 Array[Index] = Temp;

Also when object becomes more complex and they have constructors. Initializing them only when you need them can become a good savor of space time.

###Prefer pre-increment

Prefer pre-increment

You seem to use post increment. This is fine when the type is an integer. But in C++ code you usually see this with iterators and here the post increment is slightly less efficient than the pre-increment.

By using pre-increment you will always have the most efficient version and when your code is altered (and it will be) the person changing it does not need to go and check all the places where you use increment and change it from post to pre.

###Iterators

Currently you can sort anything that uses int*. But an iterator is a data type that has the same behavior as a pointer (when used in appropriate contexts). But there are other types that are iterator; so using them makes your code much more generalized.

template<typename I>
int QuickSort(I begin, I end)

Now your sort is generalized for any type that supports iterators (a pointer can be used as an iterator into a C-Array). But you can now also sort vector's lists and any other standard container type.

###Don't Declare variables before you use them

int Temp = 0; //Variable used for swaping array members

This is declared way at the top.
You should declare variables just before you need them. This helps in the readability of the code (as I don't have to scroll back to the top of the function to find out what the code type is).

Also it prevents you wasting an instruction initializing a value that you will never use (here you init to zero but is that just a waste). If you declare at the point of usage you avoid that waste.

 int Temp = Array[Wall]; // declaration and initialization
 Array[Wall] = Array[Index];
 Array[Index] = Temp;

Also when object becomes more complex and they have constructors. Initializing them only when you need them can become a good savor of space time.

###Prefer pre-increment

You seem to use post increment. This is fine when the type is an integer. But in C++ code you usually see this with iterators and here the post increment is slightly less efficient than the pre-increment.

By using pre-increment you will always have the most efficient version and when your code is altered (and it will be) the person changing it does not need to go and check all the places where you use increment and change it from post to pre.

Iterators

Currently you can sort anything that uses int*. But an iterator is a data type that has the same behavior as a pointer (when used in appropriate contexts). But there are other types that are iterator; so using them makes your code much more generalized.

template<typename I>
int QuickSort(I begin, I end)

Now your sort is generalized for any type that supports iterators (a pointer can be used as an iterator into a C-Array). But you can now also sort vector's lists and any other standard container type.

Don't Declare variables before you use them

int Temp = 0; //Variable used for swaping array members

This is declared way at the top.
You should declare variables just before you need them. This helps in the readability of the code (as I don't have to scroll back to the top of the function to find out what the code type is).

Also it prevents you wasting an instruction initializing a value that you will never use (here you init to zero but is that just a waste). If you declare at the point of usage you avoid that waste.

 int Temp = Array[Wall]; // declaration and initialization
 Array[Wall] = Array[Index];
 Array[Index] = Temp;

Also when object becomes more complex and they have constructors. Initializing them only when you need them can become a good savor of space time.

Prefer pre-increment

You seem to use post increment. This is fine when the type is an integer. But in C++ code you usually see this with iterators and here the post increment is slightly less efficient than the pre-increment.

By using pre-increment you will always have the most efficient version and when your code is altered (and it will be) the person changing it does not need to go and check all the places where you use increment and change it from post to pre.

Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

###Iterators

Currently you can sort anything that uses int*. But an iterator is a data type that has the same behavior as a pointer (when used in appropriate contexts). But there are other types that are iterator; so using them makes your code much more generalized.

template<typename I>
int QuickSort(I begin, I end)

Now your sort is generalized for any type that supports iterators (a pointer can be used as an iterator into a C-Array). But you can now also sort vector's lists and any other standard container type.

###Don't Declare variables before you use them

int Temp = 0; //Variable used for swaping array members

This is declared way at the top.
You should declare variables just before you need them. This helps in the readability of the code (as I don't have to scroll back to the top of the function to find out what the code type is).

Also it prevents you wasting an instruction initializing a value that you will never use (here you init to zero but is that just a waste). If you declare at the point of usage you avoid that waste.

 int Temp = Array[Wall]; // declaration and initialization
 Array[Wall] = Array[Index];
 Array[Index] = Temp;

Also when object becomes more complex and they have constructors. Initializing them only when you need them can become a good savor of space time.

###Prefer pre-increment

You seem to use post increment. This is fine when the type is an integer. But in C++ code you usually see this with iterators and here the post increment is slightly less efficient than the pre-increment.

By using pre-increment you will always have the most efficient version and when your code is altered (and it will be) the person changing it does not need to go and check all the places where you use increment and change it from post to pre.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /