首页 > 其他分享 >cpp: Sorting Algorithms

cpp: Sorting Algorithms

时间:2023-09-28 15:13:13浏览次数:41  
标签:Sorting int void param Algorithms cpp array include size

 

/*****************************************************************//**
 * \file   SortingAlgorithms.h
 * \brief  排序
 * \ IDE  vs 2022 C++ 20
 * \author geovindu
 * \date   September 28 2023
 *********************************************************************/
#pragma once

#ifndef SORTHINALGORITHMS_H 
#define SORTHINALGORITHMS_H 

#include <iostream>
#include <vector>


using namespace std;

namespace GeovinDu
{

    /**
     * 排序.
     */
	class SortingAlgorithms
	{

    private:


    public:

        /**
         * .
         * 
         * \param a
         * \param b
         */
        //void swap(int* a, int* b);

        /**
         * 1.Bubble Sort冒泡排序法  也可以用int array[]
         *
         * \param array int数组
         * \param size 数组长度
         */
		void BubbleSort(int array[],int size);
        /**
         * .
         * 
         * \param array
         */
        void printArray(int array[], int size);
        /**
         * 1.Bubble Sort冒泡排序法.
         * , int size
         * \param arr
         * \param n
         */
        void BubbleSort2(int arr[]);

        /**
         * 1.Bubble Sort冒泡排序法.
         * 
         * \param a
         */
        void BubbleSortVect(vector<int>& a);

        /**
         * .
         * 
         * \param va
         */
        void printVector(vector<int> va);
        /**
         * 2. Selection Sort选择排序.
         * 
         * \param array
         * \param size
         */
        void SelectionSort(int array[], int size);

        /**
         *3. Insertion Sort插入排序 .
         * 
         * \param array
         * \param size
         */
        void InsertionSort(int array[], int size);



	};

}

#endif

  

/*****************************************************************//**
 * \file   SortingAlgorithms.cpp
 * \brief  排序
 * 
 * \ IDE  vs 2022 C++ 20
 * \author geovindu
 * \date   September 28 2023
 *********************************************************************/


#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h> 
#include <vector>

#include "SortingAlgorithms.h"

using namespace std;


namespace GeovinDu
{




    /**
     * .
     * 
     * \param a
     * \param b
     */
    /*void SortingAlgorithms::swap(int* a, int* b) {
        int temp = *a;
        *a = *b;
        *b = temp;
    }*/

    /**
     * 1.Bubble Sort冒泡排序法  也可以用int array[]
     *  用法有问题
     * \param array int数组
     * \param size 数组长度
     */
    void SortingAlgorithms::BubbleSort(int array[],int size) {


        //int size = sizeof(array) / sizeof(array[0]);

        // loop to access each array element
        for (int step = 0; step < size; ++step) {

            // loop to compare array elements
            for (int i = 0; i < size - step; ++i) {

                // compare two adjacent elements
                // change > to < to sort in descending order
                if (array[i] > array[i + 1]) {

                    // swapping elements if elements
                    // are not in the intended order
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
    }
    /**
     * Bubble Sort冒泡排序法 .
     * , int size
     * \param array
     * \param size
     */
    void SortingAlgorithms::BubbleSort2(int array[])
    {
        int i, j;
        int size = sizeof(array) / sizeof(array[0]);
        bool swapped;
        for (i = 0; i < size - 1; i++) {
            swapped = false;
            for (j = 0; j < size - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array[j], array[j + 1]);
                    swapped = true;
                }
            }

            // If no two elements were swapped
            // by inner loop, then break
            if (swapped == false)
                break;
        }
    }

    /**
     * Bubble Sort冒泡排序法 .
     * 
     * \param a
     */
    void SortingAlgorithms::BubbleSortVect(vector<int>& a)
    {

        bool swap = true;
        while (swap) {
            swap = false;
            for (size_t i = 0; i < a.size() - 1; i++) {
                if (a[i] > a[i + 1]) {
                    a[i] += a[i + 1];
                    a[i + 1] = a[i] - a[i + 1];
                    a[i] -= a[i + 1];
                    swap = true;
                }
            }
        }
    }
    /**
     * .
     * 
     * \param array
     */
    void SortingAlgorithms::printArray(int array[], int size) {

       // int size = sizeof(array) / sizeof(array[0]);
       // printf("%d",size);

        for (int i = 0; i < size; ++i) {
            cout << "  " << array[i];
        }
        cout << "\n";
    }

    /**
     * .
     * 
     * \param va
     */
    void SortingAlgorithms::printVector(vector<int> va) {

        for (size_t i = 0; i < va.size(); i++) {

            cout << va[i] << " ";
        }
        cout << endl;

    }

    /**
     * 2. Selection Sort选择排序.
     * 
     * \param array
     * \param size
     */
    void SortingAlgorithms::SelectionSort(int array[], int size) {

        /*
        bool swapped;
        int i;
        for (int step = 0; step < size - 1; step++) {
            int min_idx = step;
            swapped = false;
            for (i = step + 1; i < size; i++) {

                // To sort in descending order, change > to < in this line.
                // Select the minimum element in each loop.
                if (array[i] < array[min_idx])
                    min_idx = i;
            }

            // put min at the correct position
            if (min_idx != i)
                swap(&array[min_idx], &array[step]);

        

        }
        */

        int i, j, min_idx;

        // One by one move boundary of
        // unsorted subarray
        for (i = 0; i < size - 1; i++) {

            // Find the minimum element in
            // unsorted array
            min_idx = i;
            for (j = i + 1; j < size; j++) {
                if (array[j] < array[min_idx])
                    min_idx = j;
            }

            // Swap the found minimum element
            // with the first element
            if (min_idx != i)
                swap(array[min_idx], array[i]);
        }


    }

    /**
     * 3.Insertion Sort插入排序.
     * 
     * \param array
     * \param size
     */
    void SortingAlgorithms::InsertionSort(int array[], int size) {


        for (int step = 1; step < size; step++) {
            int key = array[step];
            int j = step - 1;

            // Compare key with each element on the left of it until an element smaller than
            // it is found.
            // For descending order, change key<array[j] to key>array[j].
            while (key < array[j] && j >= 0) {
                array[j + 1] = array[j];
                --j;
            }
            array[j + 1] = key;
        }
    }




}

  

/*****************************************************************//**
 * \file   geovin.h
 * \brief  排序
 * \ IDE  vs 2022 C++ 20
 * \author geovindu
 * \date   September 28 2023
 *********************************************************************/
#pragma once

#ifndef GEOVIN_H 
#define GEOVIN_H 

#include <iostream>
#include "SortingAlgorithms.h"


using namespace std;



namespace GeovinDu
{

	/**
	 * 排序实例
	 */
	class geovin
	{
	
	private:

	public:

		/**
		 * 1.Bubble Sort冒泡排序法.
		 * 
		 */
		void bubble();

		/**
		 * 2. Selection Sort选择排序..
		 * 
		 */
		void Selection();
		/**
		 *3.Insertion Sort插入排序.
		 * 
		 */
		
		void Insertion();


	};


}
#endif

  

/*****************************************************************//**
 * \file   geovin.cpp
 * \brief  排序
 * \ IDE  vs 2022 C++ 20
 * \author geovindu
 * \date   September 28 2023
 *********************************************************************/

#include "geovin.h"
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h> 
#include <vector>
#include "SortingAlgorithms.h"


namespace GeovinDu
{

	/**
	 * 1.Bubble Sort冒泡排序法.
	 * 
	 */
	void geovin::bubble()
	{
		//非静态
		SortingAlgorithms sort;
		
		int data[] = { -2, 45, 0, 11, -9 };

		// find array's length
		int size = sizeof(data) / sizeof(data[0]);
		printf("%d", size);
		sort.BubbleSort2(data);

		cout << "1.冒泡排序法 Bubble Sorted Array in Ascending Order:\n";
		sort.printArray(data,size);

		cout << "1.冒泡排序法 用vector:\n";
		vector<int> va{ 2,9,0,4,5,1,8,7 };
		sort.printVector(va);


	}
	/**
	 * 2. Selection Sort选择排序..
	 * 
	 */
	void geovin::Selection()
	{
		SortingAlgorithms sort;
		int geovindu[] = { 20, 12, 10, 15, 2 };
		int size = sizeof(geovindu) / sizeof(geovindu[0]);
		sort.SelectionSort(geovindu, size);
		cout << "2.Selection Sorted array in Acsending Order:\n";
		sort.printArray(geovindu, size);
	}
	/**
	* 3.Insertion Sort插入排序.
	*
	*/
	void geovin::Insertion()
	{
		SortingAlgorithms sort;
		int geovindu[] = { 9, 5, 1, 4, 3 };
		int size = sizeof(geovindu) / sizeof(geovindu[0]);
		sort.InsertionSort(geovindu, size);
		cout << "3.Insertion Sorted array in ascending order:\n";
		sort.printArray(geovindu, size);
	}

}

  

调用:

/*****************************************************************//**
 * \file   EssentialAlgorithms.cpp
 * \brief  
 * IDE  vs 2022 C++ 20
 * \author geovindu
 * \date   September 28 2023
 *********************************************************************/
#define UNICODE

#include <iostream>
#include "geovin.h"

using namespace std;
using namespace GeovinDu;



int main()
{
    std::cout << "Hello World!涂聚文 Geovin Du,geovindu,学习C++\n";
    geovin du;
    //1.
    du.bubble();
    //2.
    du.Selection();
    //3.
    du.Insertion();




}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件


#define _UNICODE

  

 

标签:Sorting,int,void,param,Algorithms,cpp,array,include,size
From: https://www.cnblogs.com/geovindu/p/17735823.html

相关文章

  • CPP面向对象笔记
    基本属性即在类中包含的一系列变量方法即在类中定义的一系列函数Public,PrivateandProtected在没有继承的情况下,private与protected效果相同即都无法在类外直接访问调用实在想要访问,加个函数就行public则可以随意访问调用static仅与类的整体全局有关不受具体哪......
  • swig/perl5/swig_wrap.cpp:763:20: fatal error: EXTERN.h: No such file or director
     001、问题 002、解决方法 (base)[[email protected]]#yum-yinstallperl-devel 参考:https://www.likecs.com/ask-702675.html。 ......
  • python: Sorting Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:PythonSortingAlgorithms#描述:*https://www.programiz.com/dsa/counting-sort#*https://www.geeksforgeeks.org/sorting-algorithms/#Author:geovindu,GeovinDu涂聚文.#IDE:PyC......
  • python: Essential Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/9/2121:28#User:geovindu#Product:PyCharm#Project:EssentialAlgor......
  • C模拟CPP的方法重写(override)和多态
    1.所谓override,就是子类中重新实现了父类中的某一方法(子类和父类的同一个方法的方法体不同)2.所谓多态,最显著的一个特点就是父类指针指向不同的子类对象时,运行同一个方法会有不同的行为3.C语言模拟继承时,父类对象必须是子类对象的第一个成员4.理解了C的父类结构体指针子类结......
  • c: Sorting Algorithms
    SortAlgorithm.h /*****************************************************************//***\fileSortAlgorithm.h*\brief业务操作方法*VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md*https://www.progra......
  • 解决安装VS2022时,出现未能安装包"Microsoft.VisualCpp.Redist.14,version=14.32.31332
    解决安装VS2022时,出现未能安装包"Microsoft.VisualCpp.Redist.14,version=14.32.31332,chip”=x86"问题描述之前安装过MSSQLServer和VS2022,但是后来又卸载了。现在重装VS2022出现两个报错:无法安装Microsoft.VisualCpp.Redist.14,version=14.32.31332,chip”=x86无法安装Micr......
  • 解决 ChatGLM.CPP+clBlast 编译错误(也适用于SD.CPP)
    首先安装OpenCL和clblast:vcpkginstallopenclclblast下载GitHub上的源码:gitclone--recurse-submoduleshttps://github.com/li-plus/chatglm.cppcdchatglm.cpp这个项目GGML子仓库中,third_party/ggml/src/CMakeList.txt有个错误。打开并找到178-183行: find_......
  • natsort.natsorted()-用于自然排序(natural sorting)字符串列表。
    参考:https://natsort.readthedocs.io/en/stable/api.html#natsort.natsorted语法格式natsort.natsorted(seq:Iterable[T],key:Optional[Callable[[T],Union[natsort.utils.SupportsDunderLT,natsort.utils.SupportsDunderGT,None]]]=None,reverse:bool=False,alg:......
  • c/cpp: main() - envp
    c/cpp: main()-envp    1. main(intargc, char*argv[], char*envp[]) -envp1[wit@fedoranull]$catenvp_main.c2#include<stdio.h>3#include<stdlib.h>456intmain(intargc,char*argv[],char*envp[])7{8int......