TAILIEUCHUNG - A Complete Guide to Programming in C++ part 73

A Complete Guide to Programming in C++ part 73. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | SOLUTIONS 699 Exercise 3 --------------------------------------------------------- Compares the performances of sorting algorithms quick sort and selection sort For this purpose two identical arrays are dynamically generated and initialized with random numbers. The times needed for sorting are displayed. --------------------------------------------------------- include iostream include iomanip include cstdlib include ctime using namespace std void isort int v int lenv For qsort extern C int intcmp const void const void main unsigned int i size int numbers1 numbers2 long timel time2 cout n The performance of the sorting algorithms n quick sort and selection sort n is being compared. n n nHow many numbers are to be sorted cin size numbersl new int size numbers2 new int size cout nThere are size random numbers to be generated. n srand unsigned time NULL Initialize the random number generator. for i 0 i size i numbers1 i numbers2 i rand Random numbers cout nSorting starts Please wait. n time time1 Length of time for quick sort. qsort numbers1 size sizeof int intcmp time time2 cout nTime taken by the quick sort algorithm time2 - timel seconds. n 700 CHAPTER 30 MORE ABOUT POINTERS cout nI am sorting again. Please wait n time time1 Length of time isort numbers2 size for selection sort time time2 cout nTime taken by the insertion sort algorithm time2 - timel seconds. n nOutput sorted numbers y n n n char c cin c if c Y c y for i 0 i size i cout setw 12 numbers1 i cout endl return 0 extern C int intcmp const void a const void b return int a - int b sorts an array of int values using the selection sort algorithm. int len minp isort void isort int a register int b int last help last a len - 1 for a last a minp a for b a 1 b if b minp minp b Sort the array a of length len in ascending order Points to the last element Search for the smallest element starting at a. minp points to the current smallest array element. last b Search for the minimum. help a a minp minp .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.