0% found this document useful (0 votes)
38 views9 pages

Int Int Int Using Namespace Int: Example of Linear Search Implementation: #Include

The document provides examples of implementations for linear search, binary search, bubble sort, selection sort, and insertion sort algorithms in C++. It includes the code for each algorithm's function with comments explaining what each part is doing.

Uploaded by

israel teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views9 pages

Int Int Int Using Namespace Int: Example of Linear Search Implementation: #Include

The document provides examples of implementations for linear search, binary search, bubble sort, selection sort, and insertion sort algorithms in C++. It includes the code for each algorithm's function with comments explaining what each part is doing.

Uploaded by

israel teshome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Example of Linear Search Implementation:

1. #include <iostream>

2. int LinearSearch(int list[ ], int key);

3. using namespace std;

4. int main()

5. {

6. int list[] = {5, 3, 7, 4, 6};

7. int k = 4;

8. int i = LinearSearch(list, k);

9. if(i==-1)

10. cout << "the search item is not found" << endl;

11. else

12. cout << "The value is found at index position " << i << endl;

13. return 0;

14. }

15.

16. int LinearSearch(int list[ ], int key)

17. {

18. int index=-1;

19. for(int i=0; i < n; i++)

20. {

21. if(list[i]==key)

22. {

23. index=i;

24. break;

25. }

26. }

27. return index;

28. }

Example of binary Search Implementation:


1. #include <iostream>

2. int BinarySearch(int list[ ], int key);

3. using namespace std;

4. int main()

5. {

6. int list[] = {15, 23, 47, 54, 76};

7. int k = 54;

8. int i = BinarySearch(list, k);

9. if(i==-1)

10. cout << "the search item is not found" << endl;

11. else

12. cout << "The value is found at index position " << i << endl;

13. return 0;

14. }

15. int BinarySearch(int list[ ], int key)

16. {

17. int found=0,index=0;

18. int top=n-1,bottom=0,middle;

19. do

20. {

21. middle=(top + bottom)/2;

22. if(key==list[middle])

23. found=1;

24. else

25. {

26. if(key < list[middle])

27. top=middle-1;

28. else bottom=middle+1;

29. }

30. }while(found==0 && top>=bottom);


31. if(found==0)

32. index=-1;

33. else

34. index=middle;

35. return index;

36. }

Example of bubble Sort Implementation:


1. #include <iostream>

2. int BinarySearch(int list[ ], int key);

3. using namespace std;

4. int main()

5. {

6. int list[] = {15, 23, 47, 54, 76};

7. int k = 54;

8. int i = BinarySearch(list, k);

9. if(i==-1)

10. cout << "the search item is not found" << endl;

11. else

12. cout << "The value is found at index position " << i << endl;

13. return 0;

14. }

15. int BinarySearch(int list[ ], int key)

16. {

17. int found=0,index=0;

18. int top=n-1,bottom=0,middle;

19. do

20. {

21. middle=(top + bottom)/2;

22. if(key==list[middle])

23. found=1;

24. else

25. {

26. if(key < list[middle])

27. top=middle-1;

28. else bottom=middle+1;

29. }

30. }while(found==0 && top>=bottom);


31. if(found==0)

32. index=-1;

33. else

34. index=middle;

35. return index;

36. }

Example of selection Sort Implementation:


1. #include <iostream>

2. using namespace std;

3. void selectionSort(int list[]);

4. int list[] = {5, 3, 7, 4, 6};

5. int main()

6. {

7. cout << "The values before sorting are: \n";

8. for(int i = 0; i < 5; i++)

9. cout << list[i] << " ";

10. selectionSort(list);

11. cout << endl;

12. cout << "The values after sorting are: \n";

13. for(int i = 0; i < 5; i++)

14. cout << list[i] << " ";

15. return 0;

16. }

17.

18. void selectionSort(int list[ ] )

19. {

20. int minIndex, temp;

21. for (int i = 0; i <= n - 2; i++)

22. {

23. minIndex = i;

24. for (int j = i + 1; j <= n-1; j++)

25. if (list[j] < list[minIndex])

26. minIndex = j;

27. if (minIndex != i)

28. {

29. temp = list[i];

30. list[i] = list[minIndex];


31. list[minIndex] = temp;

32. }

33. }

34. }

Example of insertion Sort Implementation:


1. #include <iostream>

2. using namespace std;

3. void InsertionSort(int list[]);

4. int list[] = {5, 3, 7, 4, 6};

5. int main()

6. {

7. cout << "The values before sorting are: \n";

8. for(int i = 0; i < 5; i++)

9. cout << list[i] << " ";

10. InsertionSort(list);

11. cout << endl;

12. cout << "The values after sorting are: \n";

13. for(int i = 0; i < 5; i++)

14. cout << list[i] << " ";

15. return 0;

16. }

17.

18. void InsertionSort(int list[])

19. {

20. for (int i = 1; i <= n-1; i++)

21. {

22. for(int j = i;j>=1; j--)

23. {

24. if(list[j-1] > list[j])

25. {

26. int temp = list[j];

27. list[j] = list[j-1];

28. list[j-1] = temp;

29. }

30. else break;
31. }

32. }

33. }

You might also like