没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Kalicharan
Shelve in
Programming Languages / ANSI C
User level:
Intermediate
www.apress.com
SOURCE CODE ONLINE
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Advanced Topics in C
C is the most widely used programming language of all time. It has been used to
create almost every category of software imaginable and the list keeps growing
every day. Cutting-edge applications, such as Arduino, embeddable and wearable
computing are ready-made for C.
Advanced Programming In C teaches concepts that any budding programmer
should know. You’ll delve into topics such as sorting, searching, merging, recur-
sion, random numbers and simulation, among others. You will increase the range
of problems you can solve when you learn how to manipulate versatile and popular
data structures such as binary trees and hash tables.
This book assumes you have a working knowledge of basic programming con-
cepts such as variables, constants, assignment, selection (if..else) and looping
(while, for). It also assumes you are comfortable with writing functions and working
with arrays. If you study this book carefully and do the exercises conscientiously,
you would become a better and more agile programmer, more prepared to code
today’s applications (such as the Internet of Things) in C.
With Advanced Programming In C, you will learn:
• What are and how to use structures, pointers, and linked lists
• How to manipulate and use stacks and queues
• How to use random numbers to program games, and simulations
• How to work with files, binary trees, and hash tables
• Sophisticated sorting methods such as heapsort, quicksort, and mergesort
• How to implement all of the above using C
RELATED
9 781430264002
ISBN 978-1-4302-6400-2
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
v
Contents at a Glance
About the Author ���������������������������������������������������������������������������������������������������������������xiii
About the Technical Reviewer �������������������������������������������������������������������������������������������� xv
Preface ����������������������������������������������������������������������������������������������������������������������������� xvii
Chapter 1: Sorting, Searching, and Merging ■ ���������������������������������������������������������������������1
Chapter 2: Structures ■ ������������������������������������������������������������������������������������������������������27
Chapter 3: Pointers ■ ����������������������������������������������������������������������������������������������������������51
Chapter 4: Linked Lists ■ ����������������������������������������������������������������������������������������������������69
Chapter 5: Stacks and Queues ■ ���������������������������������������������������������������������������������������103
Chapter 6: Recursion ■ �����������������������������������������������������������������������������������������������������133
Chapter 7: Random Numbers, Games, and Simulation ■ ��������������������������������������������������159
Chapter 8: Working with Files ■ ���������������������������������������������������������������������������������������183
Chapter 9: Introduction to Binary Trees ■ ������������������������������������������������������������������������213
Chapter 10: Advanced Sorting ■ ���������������������������������������������������������������������������������������241
Chapter 11: Hashing ■ ������������������������������������������������������������������������������������������������������265
Index ���������������������������������������������������������������������������������������������������������������������������������287
1
Chapter 1
Sorting, Searching, and Merging
In this chapter, we will explain the following:
How to sort a list of items using selection and insertion sort•
How to add a new item to a sorted list so that the list remains sorted•
How to sort an array of strings•
How to sort related (parallel) arrays•
How to search a sorted list using • binary search
How to search an array of strings•
How to write a program to do a frequency count of words in a passage•
How to merge two sorted lists to create one sorted list•
1.1 Sorting an Array: Selection Sort
Sorting is the process by which a set of values are arranged in ascending or descending order. There are many reasons
to sort. Sometimes we sort in order to produce more readable output (for example, to produce an alphabetical listing).
A teacher may need to sort her students in order by name or by average score. If we have a large set of values and we
want to identify duplicates, we can do so by sorting; the repeated values will come together in the sorted list.
Another advantage of sorting is that some operations can be performed faster and more efficiently with sorted
data. For example, if data is sorted, it is possible to search it using binary search—this is much faster than using a
sequential search. Also, merging two separate lists of items can be done much faster than if the lists were unsorted.
There are many ways to sort. In this chapter, we will discuss two of the “simple” methods: selection and insertion
sort. In Chapter 10, we will look at more sophisticated ways to sort. We start with selection sort.
Consider the following list of numbers stored in a C array, num:
Chapter 1 ■ Sorting, SearChing, and Merging
2
Sorting num in ascending order using selection sort proceeds as follows:
1
st
pass
Find the smallest number in the entire list, from positions • 0 to 6; the smallest is 15, found
in position 4.
Interchange the numbers in positions • 0 and 4. This gives us the following:
2
nd
pass
Find the smallest number in positions • 1 to 6; the smallest is 33, found in position 5.
Interchange the numbers in positions • 1 and 5. This gives us the following:
3
rd
pass
Find the smallest number in positions • 2 to 6; the smallest is 48, found in position 5.
Interchange the numbers in positions • 2 and 5. This gives us the following:
4
th
pass
Find the smallest number in positions • 3 to 6; the smallest is 52, found in position 6.
Interchange the numbers in positions • 3 and 6. This gives us the following:
5
th
pass
Find the smallest number in positions • 4 to 6; the smallest is 57, found in position 4.
Interchange the numbers in positions • 4 and 4. This gives us the following:
剩余303页未读,继续阅读
资源评论
yongyongdoubuxinga
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功