SOLUTION MANUAL
Data Structures and Algorithm Analysis in C++, 4th edition
by Mark Weiss
LU
XE
LI
BR
AR
Y
, TABLE OF CONTENT
Chapter 1 Programming: A General Overview
Chapter 2 Algorithm Analysis
Chapter 3 Lists, Stacks, and Queues
Chapter 4 Trees
Chapter 5 Hashing
Chapter 6 Priority Queues (Heaps)
Chapter 7 Sorting
LU
Chapter 8 The Disjoint Sets Class
Chapter 9 Graph Algorithms
Chapter 10 Algorithm Design Techniques
XE
Chapter 11 Amortized Analysis
Chapter 12 Advanced Data Structures and Implementation
LI
BR
AR
Y
, KLJHGFDSA
CHAPTER 1
Introduction
1.1
/*
Exercise 1.1 All Chapters Included
Selection of integers with k = N/2
select1 => sorting and selecting
LU
select2 => keeping top k
*/ All Answers Included
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
XE
#include <algorithm>
using namespace std;
void sort(vector<int> & vec)
{ // bubble sort ascending
LI
bool sorted = false;
while (!sorted)
{
sorted = true;
for (auto i = 1; i < vec.size(); i++)
BR
{
if (vec[i-1]> vec[i])
{
swap(vec[i],vec[i-1]);
sorted = false;
}
}
}
AR
}
void sortDec(vector<int> & vec)
{ // bubble sort descending
bool sorted = false;
while (!sorted)
{
Y
sorted = true;
for (auto i = 1; i < vec.size(); i++)
{
if (vec[i-1]< vec[i])
{
swap(vec[i],vec[i-1]);
sorted = false;
}
}
}
}
kjhgfdsa
, KLJHGFDSA
int select1(vector<int> nums)
{
int k = (nums.size()+1)/2;
sort(nums);
return nums[k];
}
int select2(const vector<int> &nums)
{
int k = nums.size()/2;
LU
vector<int> topK(nums.begin(), nums.begin() + k);
sortDec(topK);
for (auto i = k; i < nums.size(); i++)
{
if (nums[i] > topK[k-1])
{
XE
for (auto j = k-2; j >=0 ; j--)
if (nums[i] < topK[j])
{topK[j+1] = nums[i]; break;}
else
topK[j+1] = topK[j];
if (topK[0] < nums[i])
topK[0] = nums[i];
}
LI
}
return topK[k-1];
}
BR
int main()
{
vector<int> nums;
int selected;
time_t start, end;
srand(time(NULL));
for (auto numInts = 1000; numInts<=10000; numInts+=1000)
AR
// sizes 1,000, 2,000, 3,000, ...10,000
{
nums.resize(numInts);
start = time(NULL);
for (auto i = 0; i < 10; i++) // run 10 times
{
for (auto j = 0; j < numInts; j++)
Y
nums[j] = rand()%(2*numInts);
selected = select1(nums); // or selected = select2(nums);
}
end = time(NULL);
cout<<numInts<<"\t"<<difftime(end,start)<<endl;
}
return 0;
}
kjhgfdsa
Data Structures and Algorithm Analysis in C++, 4th edition
by Mark Weiss
LU
XE
LI
BR
AR
Y
, TABLE OF CONTENT
Chapter 1 Programming: A General Overview
Chapter 2 Algorithm Analysis
Chapter 3 Lists, Stacks, and Queues
Chapter 4 Trees
Chapter 5 Hashing
Chapter 6 Priority Queues (Heaps)
Chapter 7 Sorting
LU
Chapter 8 The Disjoint Sets Class
Chapter 9 Graph Algorithms
Chapter 10 Algorithm Design Techniques
XE
Chapter 11 Amortized Analysis
Chapter 12 Advanced Data Structures and Implementation
LI
BR
AR
Y
, KLJHGFDSA
CHAPTER 1
Introduction
1.1
/*
Exercise 1.1 All Chapters Included
Selection of integers with k = N/2
select1 => sorting and selecting
LU
select2 => keeping top k
*/ All Answers Included
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
XE
#include <algorithm>
using namespace std;
void sort(vector<int> & vec)
{ // bubble sort ascending
LI
bool sorted = false;
while (!sorted)
{
sorted = true;
for (auto i = 1; i < vec.size(); i++)
BR
{
if (vec[i-1]> vec[i])
{
swap(vec[i],vec[i-1]);
sorted = false;
}
}
}
AR
}
void sortDec(vector<int> & vec)
{ // bubble sort descending
bool sorted = false;
while (!sorted)
{
Y
sorted = true;
for (auto i = 1; i < vec.size(); i++)
{
if (vec[i-1]< vec[i])
{
swap(vec[i],vec[i-1]);
sorted = false;
}
}
}
}
kjhgfdsa
, KLJHGFDSA
int select1(vector<int> nums)
{
int k = (nums.size()+1)/2;
sort(nums);
return nums[k];
}
int select2(const vector<int> &nums)
{
int k = nums.size()/2;
LU
vector<int> topK(nums.begin(), nums.begin() + k);
sortDec(topK);
for (auto i = k; i < nums.size(); i++)
{
if (nums[i] > topK[k-1])
{
XE
for (auto j = k-2; j >=0 ; j--)
if (nums[i] < topK[j])
{topK[j+1] = nums[i]; break;}
else
topK[j+1] = topK[j];
if (topK[0] < nums[i])
topK[0] = nums[i];
}
LI
}
return topK[k-1];
}
BR
int main()
{
vector<int> nums;
int selected;
time_t start, end;
srand(time(NULL));
for (auto numInts = 1000; numInts<=10000; numInts+=1000)
AR
// sizes 1,000, 2,000, 3,000, ...10,000
{
nums.resize(numInts);
start = time(NULL);
for (auto i = 0; i < 10; i++) // run 10 times
{
for (auto j = 0; j < numInts; j++)
Y
nums[j] = rand()%(2*numInts);
selected = select1(nums); // or selected = select2(nums);
}
end = time(NULL);
cout<<numInts<<"\t"<<difftime(end,start)<<endl;
}
return 0;
}
kjhgfdsa