9
Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “ .

Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Embed Size (px)

Citation preview

Page 1: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

Instructor : Prof. Jyh-Shing Roger Jang

Designer : Shao-Huan WangThe ideas are reference to the textbook “Fundamentals of Data Structures in C “ .

Page 2: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

26 5 37 1 61 11 59 15 48 19 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.

Page 3: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

26 5 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

Page 4: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

Page 5: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

Page 6: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j < i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

If j = pivot key, don’t change.

Page 7: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

If j = pivot key, don’t change.

Page 8: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

If j = pivot key, don’t change.

Page 9: Quick Sort Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures

Quick Sort

R0 1 2 3 4 5 6 7 8 9left

right

Set an array and add left and right to record the first and last indexes.

Take a pivot key at first term.

265 371 6111 5915 4819 0 9

Scan from left to right, if the element larger than pivot key,finding a smaller item from right to left.If j <= i (j : right to left; i : left to right), change the j and pivot key.Take the left and right array of pivot key to do the same way

If j = pivot key, don’t change.